log rotate

We need to use a utility called logrotate

To see if it is installed

logrotate --version

Config files

  • /etc/logrotate.conf:
    • this file contains some default settings and sets up rotation for a few logs that are not owned by any system packages. It also uses an include statement to pull in configuration from any file in the /etc/logrotate.d directory.
  • /etc/logrotate.d/:
    • this is where any packages you install that need help with log rotation will place their Logrotate configuration. On a standard install you should already have files here for basic system tools like apt, dpkg, rsyslog and so on.

Create a sample app log rotator

vi sudo /etc/logrotate.d/example-app

/var/log/example-app/*.log {
    daily
    missingok
    rotate 14
    compress
    notifempty
    create 0640 www-data www-data
    sharedscripts
    postrotate
        systemctl reload example-app
    endscript
}

Creating an Independent Logrotate Configuration

In this example we have an app running as our user sammy, generating logs that are stored in /home/sammy/logs/. We want to rotate these logs hourly, so we need to set this up outside of the /etc/logrotate.d structure provided by Ubuntu.

So

vi /home/sammy/logrotate.conf

In this file put

/home/sammy/logs/*.log {
    hourly
    missingok
    rotate 24
    compress
    create
}

If you are outputting data from stdio ... you may need to change a parameter copytruncate - this allows the stdio data to continue to flow.

/home/tim/mine.log {
    daily 
    copytruncate
    missingok
    rotate 1
    compress
    create
}

Setting Logrotate and logstate

We now need to ask the daemon service to use our logrotate config and to generate a logstate file.

logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state --verbose

To see what the state of the logrotate is

cat /home/sammy/logrotate-state

Force a LogRotate

logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state --verbose --force

Schedule a LogRotate

Using the users CRON setting - here running every 14 mins of every house

14 * * * * /usr/sbin/logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state

Eth Miner Example

This is my config file

rotate 4
/home/tim/mine.log {
    hourly
    missingok
    copytruncate
    rotate 24
    compress
    create
}

To set it up

logrotate /home/tim/logrotate.conf --state /home/tim/logrotate-state --verbose
reading config file /home/tim/logrotate.conf

Output is

Handling 1 logs

rotating pattern: /home/tim/mine.log  hourly (24 rotations)
empty log files are rotated, old logs are removed
considering log /home/tim/mine.log
  log does not need rotating

Crontab entry