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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
harp
  • 1,017
  • 2
  • 13
  • 16

2 Answers2

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
}
Jenny D
  • 13,022
  • 3
  • 38
  • 54
  • 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
  • 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 '12 at 10:17
  • Works like a charm :) – harp Sep 10 '12 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 '15 at 22:48
  • Other than the possibility that some data may be lost, are there any other known downsides to using `copytruncate`? – Leo Galleguillos Apr 24 '19 at 01:17
  • @LeoGalleguillos If you're short of disk space it could be an issue, since you'll be duplicating the old log file before truncating it, which means it will take up twice as much space while the copying is going on. – Jenny D Apr 24 '19 at 06:08
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
Boris Ivanov
  • 247
  • 2
  • 7
  • 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 '12 at 09:48
  • Looks like a good alternative. Does cronolog compress on-the-fly? – harp Sep 10 '12 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. – Arie Skliarouk Oct 24 '18 at 10:52