Apache – How to avoid apache reload when rotating logs

apache-httpdlogrotate

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?

Best Answer

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
}
Related Question