28

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 any more in the just-rotated logfile. Is there a way to avoid Apache reloads every time logrotate does a rotation?

2 Answers 2

48

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
}
6
  • Thanks for your answer. So I guess when I add lastaction echo "" | /apache/*log endscript the filehandle is not "lost"?
    – harp
    Sep 10, 2012 at 10:08
  • 3
    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, 2012 at 10:17
  • Works like a charm :)
    – harp
    Sep 10, 2012 at 10:56
  • 4
    @harp be careful, logrotate doc says: "Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost."
    – Totor
    Apr 23, 2015 at 22:48
  • Other than the possibility that some data may be lost, are there any other known downsides to using copytruncate? Apr 24, 2019 at 1:17
5

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
3
  • 1
    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, 2012 at 9:48
  • Looks like a good alternative. Does cronolog compress on-the-fly?
    – harp
    Sep 10, 2012 at 10:59
  • There is similar app "rotatelogs" in apache2-utils package. Just be careful not to "pipe" into same log file from different apache virtual servers - they will stomp on each other. Oct 24, 2018 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .