-----BEGIN PGP SIGNED MESSAGE----- LOG_ROTATE : Cron-based utility to rotate Apache logs at the end of month. AUTHOR : Cancer Omega VERSION : 1.4 (Released 12/06/2000; Bug fixes 02/02/2001) DEDICATION : Dedicated to Satrina. She 0wnz m3. R00ted the heart port. CONTENTS : comega.asc My PGP public key (to check signatures) log_rotate.sh Shell script log_rotate.sh.asc PGP signature for script log_rotate.txt.asc PGP signed info file NEWBIE NOTE: This script is meant for use via cron(1M). Do a 'man cron' and 'man crontab' if you are not familiar with the service. The root user should have a crontab entry in the form of: 59 23 28-31 * * /root/log_rotate BACKGROUND : This utility was created out of sheer annoyance. Simply put, I was annoyed by the limitations Apache's rotatelogs (I don't want a new access_log every 24 hours), and I was equally irritated with cron(1M)'s inability to natively schedule tasks that need to occur on the last day of any given month. The commonly-accepted practice of using separate cron entries for each month was no solution as it could not take into account the issue of Leap Days, as was the case with 2000 and will be again in 2004. With the above in mind, I wrote a simple shell script that can determine if a given day *is* the last day of any given month. Upon satisfying that condition, the script executes command sequences to archive the past month's Apache access and error logs during the last minute of that month. This script can be readily modified to suit any other end-of-month processing the operator should need to perform. This is released as an Apache log rotation utility only because it was developed as such. REQUIREMENT: This script utilizes the following binaries. Be sure you have these loaded on your system: BINARY ORIGIN ----------------- ----------------------------------------- apachectl Standard with Apache v1.3.x and higher cat, cp, cut, Standard with all UNIX flavors date, echo, expr, ls, and sleep gzip GNUzip - ftp://ftp.gnu.org/, though newer UNIX distros have it installed Modify the script to use compress(1) if you like. IMPORTANT : Some variables may need to be changed to suit your system. These variables are flagged in script comments with a '*'. This script was developed using the Bourne shell on Solaris 2.7 (x86). Users of other flavors of UNIX (Linux, IRIX, AIX, et cetera) should check the locations of binaries called in this script. Do a 'which ' to locate the files and make script adjustments where necessary. EXPLANATION: Here's what this beast does with the appropriate cron entry: 1. Stops Apache at 23:59 hours on the last day of the month. 2. Lists the file sizes of the existing Apache access and error logs. 3. Performs a copy with date preservation of the past month's Apache access log. (Script will stop what it's doing and restart Apache if this is not successful.) 4. Empties the default access log following file copy. 5. Performs a copy with date preservation of the past month's Apache error log. (Script will stop what it's doing and restart Apache if this is not successful.) 6. Empties the default error log following file copy. 7. Calculates the number of seconds left until the start of the next minute and sleeps for that amount of time. (This is done so the new log file will only have entries of the *new* month and won't have visits from the final seconds of the old month.) 8. Restarts Apache. 9. Compresses (using gzip) the archived access log. 10. Compresses (using gzip) the archived error log. EXAMPLE: The following is an example report of a successful run. Performing monthly httpd log archival procedure... Thu Nov 30 23:59:01 PST 2000 1. Stopping Apache... /usr/local/apache/bin/apachectl stop: httpd stopped 2. Archiving logfiles for November 2000... 1699096 Nov 30 23:58 /usr/local/apache/logs/access_log 612870 Nov 30 03:38 /usr/local/apache/logs/error_log 3. /usr/bin/cp -p /usr/local/apache/logs/access_log /usr/local/apache/logs/access_log_2000-11-30 4. /usr/bin/cat /dev/null > /usr/local/apache/logs/access_log 5. /usr/bin/cp -p /usr/local/apache/logs/error_log /usr/local/apache/logs/error_log_2000-11-30 6. /usr/bin/cat /dev/null > /usr/local/apache/logs/error_log 7. Sleeping for 52 seconds... 8. Restarting Apache... /usr/local/apache/bin/apachectl start: httpd started Fri Dec 1 00:00:01 PST 2000 9. Compressing /usr/local/apache/logs/access_log_2000-11-30...done. 82804 Dec 1 00:00 /usr/local/apache/logs/access_log_2000-11-30.gz 10. Compressing /usr/local/apache/logs/error_log_2000-11-30...done. 42414 Dec 1 00:00 /usr/local/apache/logs/error_log_2000-11-30.gz Run complete. IN CLOSING : That's it in a nutshell. Please direct feedback to me at comega@attrition.org. Please seek answers to your questions in this document *before* sending them to me. If you encounter errors in running this script, gather *all* data of the error report (files missing, cannot execute, etc) and send those in with your question. Messages which basically say "I can't get it to work" will be ignored. Cancer Omega, 12/06/2000 comega@attrition.org comega@treachery.net -----BEGIN PGP SIGNATURE----- Version: 2.6.2 Comment: E-mail me for my PGP Public Key. iQCVAwUBOnufOJbw4Q5hr1BZAQFYdQP+MWDYVOzSA3ExwuDEQEO8BqE4dqoUlZ1P MA3I1B9R33kvCSridlFj46Q9UXcqrLVAv6Nt0Il1jtWWsoacYluwo0DVb0iqrkKj vVDrtPvSYwbD34G5mQO1rvk6BNOHenwVdhRZV8vuVF5qg0mfeH4ujKks5VvhPA9r IrShjhHuhQo= =ILyh -----END PGP SIGNATURE-----