The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
Article Feedback - Swapping boot drives around
 New Topic  |  Go to Top  |  Go to Topic  |  Search  |  Log In   Newer Topic  |  Older Topic 
 Shell script with Webalizer
Author: Loki 
Date:   20-11-01 20:07

If the real reason you want to rotate logs is because you want to keep your /var size down, and you have a web stats package in your crontab, something like the script I use may be what you want.

I have the following shell script in my crontab; it runs daily, not long after Webalizer, which also runs daily. It renames the main Apache log, restarts Apache, sleeps for ten minutes, then copies the last 100 lines of the log back to the original logfile.

The 600 is probably much higher than necessary, but it doesn't really matter. Also, my webserver is fairly low-traffic and I don't often get 100 hits in between the time Webalizer and this script run, both in the middle of the night. :) You have to have some sleep time in there, though, so Apache has time to restart and recreate the logfile.

Webalizer also doesn't care if the logfile entries are out of order, as it reads their times from the entries themselves. It will also ignore duplicate records, which will often happen with this shell script (and in fact should happen; if you're not getting duplicate records when you run Webalizer - or whatever you use - increase the number in the tail line; you're losing hits in your stats.)


mv /var/log/apache/main-access.log /var/log/apache/
kill -USR1 `cat /var/run/`
sleep 600
tail -n 100 /var/log/apache/ >> /var/log/apache/main-access.log
rm /var/log/apache/

Reply To This Message
 Re: Shell script with Webalizer
Author: Dan Langille 
Date:   21-11-01 18:33

Why bother copying the 100 lines over?

Reply To This Message
 Forum List  |  Threaded View   Newer Topic  |  Older Topic 

 User Login
 User Name:
 Remember my login:
 Forgot Your Password?
Enter your email address or user name below and a new password will be sent to the email address associated with your profile.
How to get the most out of the forum