Wednesday, September 18, 2013

Manage log files from getting too big

Log files in Linux can be managed via the logrotate command (See man pages). If the log files start growing too big, it can be difficult to audit these log files.

Here is an example to create a new log file each week as the weekly cron (See man pages) service runs.

Following is an example where the cakephp application is installed. It will create a new file each week for 52 times before deleting the oldest file. Each file will be appended with a running number starting with "1". If the log file is missing, no errors will be printed/emailed and nothing will be done if the log file is empty.

Step 1: Create the configuration file

Create the file /etc/logrotate.d/cakephp with permission 644 (owned by root) and following contents;

/var/www/html/mymesyuarat/*/tmp/logs/*.log {
    rotate 52
    create 640 root root

Step 2: Ensure that the logrotate service is running.

The above configuration can be tested with the following command;

logrotate -s /tmp/logrotatestatus /etc/logrotate/cakephp

No comments:

Blog Archive