The logrotate utilility
Under Unix, log files are generated by both the system and by various application packages such as database and web servers. These log files can grow so large over time that they become difficult to work with, and also use a lot of system resources. The logrotate utility can help to keep these files more manageable. It renames the log file after a specified period, and creates an empty one. The renamed files can be either compressed, backed up or deleted. It also allows the administrator to limit the number and size of the log files. This program is usually run daily as a cron job.
A configuration file /etc/logrotate.conf allows the administrator to configure basic settings for logrotate. Further configuration settings for each individual log are then placed in the directory /etc/logrotate.d, and given the name of the process.
An example of a typical configuration script /etc/logrotate.d/myprog is shown below:
/var/log/myprog.log {
rotate 7
daily
errors root@localhost
missingok
postrotate
/ etc/init.d/myprog restart >dev/null
endscript
}
Notes:
/var/log/myprog.log - the name of the log file to be rotated
rotate 7 - causes it to keep 7 sets of rotation logs
daily - indicates that logs are to be rotated daily
errors root@localhost - causes errors to be mailed to root on the local machine
postrotate - contains a command to stop and restart the relevant service
This should be used in conjunction with an archiving strategy that backs up the log files to tape on a regular basis. In the above example, a weekly archive would be adequate, since we are keeping seven sets of log files on disk.