logrotate : handle log file rotations

log files can get very big if not rotated at appropriate periods. logrotate is the tool that usually comes installed w/ Ubuntu boxes to do this for you.

Install logrotate

$ sudo apt-get install logrotate

Configuration

$ cat /etc/logrotate.conf

Usually you may not have to make any changes to this. Explanation for the fields can be see here. Or try man logrotate.

Application specific configurations

$ ls /etc/logrotate.d/

This folder is populated with config files from different applications even if logrotate is not installed on the machine. Example

$ cat /etc/logrotate.d/apache2

If apache2 is installed in the machine, you can see this file has the log rotate policy for all the apache log files.

Cron job

logrotate is not a daemon process. It is added a daily cron job. When everyday it is run, it checks all the log files defined in the config and updates the status file.

$ cat /var/lib/logrotate/status

When “appropriate” time has elapsed, it will create a new log file and rename the old file appropriately. It also gunzips the old file so that the disk space is saved. Its pretty neat and very simple to setup.

Tags: , , , ,

Leave a comment