Monday, August 17, 2009

Apache or IBM HTTP Server access_log rotation script

We didn't have anything in place until one day, our access_log grew to over 2gig, OUCH :). Anyway, here is a simple cron script you could setup to execute nightly.

#The script below creates a numbered copy of the log and then clears it
#The date tag at the end of the copied log rotates from 0-6 so we never keep more than 7 days of logs
#crontab is setup to run /usr/IHS/webserver*/bin/rotateaccess.sh once per day

#variables
ACCESS_HOME={path/to/your/webserver/logs}
date_tag=$(( `date +%j` % 7 ))
stdout=access_log

# rotate log
cd $ACCESS_HOME
cp $stdout $stdout.$date_tag
cp /dev/null $stdout
chown root $stdout
chgrp system $stdout
chown root $stdout.$date_tag

You could even add further compression to conserve space but I think it's not really needed at this time.