Problem: When doing a graceful restart of the apache server for log rotation, how long do you wait for the old processes to release thier filehandles on the old logfiles before you start compressing the logs?
Solution: I have scripted the log rotation script to check the filehandles of the old logfile using the UNIX utility lsof. You can set $maxtries to however many attempts you want to check the file and $sleeptime to how long you want to wait between retries. The default is 10 seconds between tries, with a maximum of 6 tries (60 seconds, because HTTP is less persistent than other protocols).
NOTES: This script is designed to be run from cron at 23:59 at night. Here is my
You can't get simpler than that.# rotate/archive the web logs # ATonns Sat Mar 31 22:46:49 EST 2001 59 23 * * * /usr/local/sbin/apachelogrotate.pl
It will get all the information about the current day/time and then sleep for 60 seconds as to attempt to rotate the logs as close to midnight as possible. A step-by-step breakdown of the procedure is:
Here is the perl source to apachelogrotate.pl.
Here is a sample config file, apachelogrotate.cfg.
Here is the GPL, which is the license for 'apachelogrotate.pl'
P.S. - I'm sure this script could use a lot of improvement. I'm leaving it as an excercise to the reader (ie: don't email me about how this isn't right or that isn't what you wanted - get off your a$$ and do it yourself). The script has a LONG history and could use an overhaul, which I'll do one of these days.