Friday, 5 December 2014

Logrotate setup


How to setup logrotate


There are several applications which do not have in build capability to rotate the logs and purge the old logs.
Lets take example of apache, You may need to rotate its log and purge old one and zip the current one. It's not possible to truncate the apache log when its running, You will need to handle in via logotate command.
Here is the way you can setup logrotate for an application.

Here I am taking example of apache, You can change it as per your need




cat /etc/logrotate.conf

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

/etc/apache2/logs/access_log {
           rotate 5
           size 10000k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

/etc/apache2/logs/ssl_request_log {
           rotate 5
           size 10000k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

/etc/apache2/logs/error_log {
           rotate 5
           size 10000k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

rotate
Log  files  are  rotated  count  times before being removed

size 
Log files are rotated when they grow bigger than size bytes

sharedscripts
prerotate and postrotate scripts are run for each log which is rotated

postrotate/endscript
 The  lines  between postrotate and endscript are executed after the log file is rotated

killall -HUP
The HUP signal ( "hangup signal") is usually sent to a program to restarts and re-reads all its configuration in the process.The Apache web server will catch a HUP signal and re-read all its configuration files but it won't restart any processes. But other process may behave differently, Some process may not catch HUP, and you may want to just use kill.

Now set a cron job as root user (or which ever can perform above job).

1 23 * * * logrotate /etc/logrotate.conf 2>&1 > /dev/null

This will run 1 pass 11pm every day.