Rotating webserver and Tomcat logs with logrotate in Debian; update AWStats before rotation

Friday September 02, 2011 ()

Log file size can affect performance of your webserver/Tomcat. Tomcat will even fail after a certain catalina.out file size. Here is how to rotate logs with logrotate.

In Debian logrotate scripts are stored in /etc/logrotate.d and is executed by a daily cron job "/etc/cron.daily/logrotate". To rotate your logs create a script in that (/etc/logrotate.d) location as "tomcatlogs" or any other name you desire only make sure you don't overwrite rotate scripts that already exist in that location.

For your Tomcat catalina.out below is a script that rotates logs weekly and maintain logs for a year. Rotated logs after 52 weeks are deleted. Don't forget to change the correct path to catalina.out.


 /opt/logs/tomcat/catalina.out { 
    weekly
    rotate 52 
    compress 
    delaycompress
    missingok
    create 640 tomcat tomcat   
 }

 /more/logs/to/rotate/can-be-in-the-script-file {
    ....
 }
	        
  

Note that a script file can contain several individual scripts. To check for errors in your script, run the following. It will run logrotate in debug mode and no changes to your logs will be made.

    
    /usr/sbin/logrotate -d /etc/logrotate.d/THE-SCRIPT-YOU-CREATED
       
       

Using prerotate and postrotate

Before and after logs are rotated sometimes you need to run a command before and after each log rotation. Below is the standard Apache2 logrotate script edited to update AWStats before each rotation. Updating AWStats before rotation will help to prevent logs loss.



 /var/log/apache2/access.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        prerotate
                /usr/lib/cgi-bin/awstats.pl 
                     -config=THE-CONFIG -update > /dev/null
        endscript
        postrotate
                /etc/init.d/apache2 reload > /dev/null
        endscript
 }
   
   

prerotate and postrotate must be terminated with endscript and must be in each separate line. See above. We also used sharedscripts to tell logrotate that both prerotate and postrotate will be executed only once.

To manually/force logs rotation use the -f option shown below.

    
    /usr/sbin/logrotate -f /etc/logrotate.d/THE-SCRIPT-YOU-CREATED
       
       

For more information about more logrotate options see logrotate man pages (man logrotate).

Good luck.


17,144

Comments (Rotating webserver and Tomcat logs with logrotate in Debian; update AWStats before rotation )