How To Manage Logfiles with Logrotate
From WickyWiki
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 logfiles, older logfiles are deleted |
| daily | Rotate not more than once a day, the state-file is used to check if it was rotated earlier today |
| 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 | gz-Compress rotated logfiles |
| delaycompress | Compress after one extra 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