Results 1 to 4 of 4

Thread: Log Rotation

  1. #1

    Log Rotation

    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.


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

  2. #2
    Join Date
    Dec 2000
    The Woodlands, Tx
    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.


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

    PHP Code:

    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"); 

  3. #3
    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.

    Glide Hosting
    Domain Names and Web Hosting
    Multi-processor, SCSI RAID, redundant, fast support

  4. #4
    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 {
            /bin/kill -HUP `cat /var/run/ 2>/dev/null` 2> /dev/null || true
    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)...
    Pure Energy Systems

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts