How To Manage Logfiles with Logrotate

From WickyWiki
Revision as of 21:10, 4 March 2019 by Wilbert (talk | contribs)


To prevent your log files from growing without limit the 'logrotate' command can be used to specify what you want to keep. There are a lot of options and you might want to integrate your log-cleaning into the daily logrotate schedule. This example however is limited to an independent configuration and some basic options.

More info here:

Complete info can be found on the man page:

man logrotate

Create logrotate config file:

sudo nano /home/user1/logs/example.conf
/home/user1/logs/*.log {
    rotate 4
    daily
    size 1k
    missingok
    compress
    delaycompress
}
chmod 0444 /home/user1/logs/example.conf

Options explained:

Option Meaning
rotate 4 Keep 4 archived logfiles, older files are deleted
daily Rotate not more than once a day, the state-file is used to check if the logfile was rotated earlier today. With these settings at least 4 complete days are kept.
size 1k Do not rotate if the logfile size is less than 1k
missingok Do not report an error if the logfile does not exist
compress Compress (gz) rotated logfiles
delaycompress Compress the last archived logfile with the next rotation

Manually logrotate

Option -v (verbose) is added so you can see what is happening.

logrotate -v /home/user1/logs/example.conf --state /home/user1/logs/logrotate-state

Add to daily crontab

The time that was used here is daily at 4:04 am.

nano /etc/crontab
 04 04 * * * *  /usr/sbin/logrotate /home/user1/logs/example.conf --state /home/user1/logs/logrotate-state