Web Hosting Talk







View Full Version : Log Rotation


nousername
07-15-2002, 09:28 PM
Hi! I need someone to show me how to write a script to be used for log rotation on apache's access_log and error_log. My servers have many requests and the logs reach about 28 megs a day.

Anybody?

With that said I would like rotation to happen once a day.

Webdude
07-15-2002, 11:53 PM
Here's a really simple method I am writing out. Put this in cgi format and run it on cron at whatever intervals you want. Set the path correct. Make an empty file in the log directory called "newlog".

After 3 script runs, you will have 3 logs. One main and two old ones.

access_log
access_log.old
access_log.old.bak

The script is expanable. Not how many would do it, but it is fast and efficient.

#!/usr/bin/perl

system ("mv -f /path/to/access_log.old /path/to/access_log.old,bak");
system ("mv -f /path/to/access_log /path/to/access_log.old");
system ("cp /path/to/newlog /path/to/access_log");

system ("mv -f /path/to/error_log.old /path/to/error_log.old,bak");
system ("mv -f /path/to/error_log /path/to/error_log.old");
system ("cp /path/to/newlog /path/to/error_log");

GH_Dave
07-16-2002, 12:28 AM
Hi Nousername,

Webdude is on the right track but messing with apache's log files while it is running will probably have some negative consequences. I am running a modified version of "logcron" and the original sounds exactly what you are looking for, as it will even compress the logs as it rotates them. It is meant o be run once a day but you can modify it if you need more rotations. Go to google, type "apache logcron" without the quotes and it's the first result (well, it was about 2 minutes ago :) ) Ahhhh... google, you are my bestest friend. ;)

Dave

Shannon
07-16-2002, 09:08 AM
if you're on a redhat style box, odds are you already have logrotate installed, which seems to do a fine job handling rotation of the apache log files (provided of course your control panel won't have a huge hiss fit over the whole idea, I'm still waiting for some confirmation on if this is okay to do with cPanel or not)..

anyway, for logrotate, RH's 7.3 installation gave me a apache file in /etc/logrotate.d that looks like this:

/var/log/httpd/access_log /var/log/httpd/agent_log /var/log/httpd/error_log /var/log/httpd/referer_log {
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
} That should (as far as I read it) rotate out your access_log, agent_log, error_log and referer_log (if they exist) on a nightly basis using your logrotate settings, and then resart httpd to insure the pointers get reset in the files...

(/etc/logrotate.conf handles the basic logrotate settings, such as when to rotate, and how many rotations to keep)...