Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use logrotate to rotate Apache access-, error- and rewrite-logs. My config file looks like this:

/apache/*log {
    compress
    dateext
    rotate 365
    size=+300M
    olddir /log/old/apache
    notifempty
    missingok
    lastaction
     /bin/apache reload
    endscript
}

My problem is that whenever a rotation occurs Apache has to be reloaded because Apache doesn't write anymore in the just rotated logfile. Is there a way to avoid Apache reloads everytime logrotate does a rotation?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

The reason that apache needs a reload is that once it's opened a file, it gets a filehandle to it, and it will keep writing to that filehandle. When you move the file, it doesn't see that, it just keeps writing to the same handle. When you do a reload, it'll open the file again and get a new handle.

To avoid the reload, instead of moving the file, you can copy it and empty the old file. That way apache can keep writing to the same filehandle. You do this by adding the option "copytruncate" to the logrotate config file, like this:

/apache/*log {
    copytruncate
    compress
    dateext
    rotate 365
    size=+300M
    olddir /log/old/apache
    notifempty
    missingok
}
share|improve this answer
    
Thanks for your answer. So I guess when I add lastaction echo "" | /apache/*log endscript the filehandle is not "lost"? –  harp Sep 10 '12 at 10:08
    
Sorry, I should have said "copytruncate" instead of "copy". Then you don't need the lastaction thing at all. I blame having too much blood in my caffeine stream :-) –  Jenny D Sep 10 '12 at 10:17
    
Works like a charm :) –  harp Sep 10 '12 at 10:56

I recommend you to use http://cronolog.org/

This is how I use it:

CustomLog     "|/usr/local/sbin/cronolog -S /var/log/httpd/t3.CCC.eu-access_log -P /var/log/httpd/t3.CCC.eu-access_log.prev /var/log/httpd/t3.CCC.eu-%Y.log" combined
share|improve this answer
    
Piping to any external program can be a problem if there's a lot of traffic to the web server. But it does neatly avoid the filehandle issue. –  Jenny D Sep 10 '12 at 9:48
    
Looks like a good alternative. Does cronolog compress on-the-fly? –  harp Sep 10 '12 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.