0

What exactly does logrotate do when it "rotates" a log file? For example, does it rename the existing file and create a new one with the old name?

This article describes restarting a service after the logs are rotated, so that it can use the new log file. Is this just because the process might keep an open handle to the old file? If my process opens a new file handle every time it writes to the log, will it use the new log file without restarting?

TS_
  • 3
  • 1
  • open and closing for each write introduces other issues, performance is of course one factor, another is race conditions. logrotate often is a separate process that renames existing logs, and then signals log deamon to reopen the files, the time between the filerename and signal allows for data to be written to the now renamed file using the existing handle. (there is no restart involved) – NiKiZe Oct 13 '21 at 04:30

1 Answers1

0

Unless the copytruncate is used, each time the log is renamed to an new name (for instance, log.0) and then empty file is created. postrotate is used to nofify the owner process about the need to reopen the log.

If process is dumb and cannot reopen its logs via signal, then copytruncate should be used - in this case the contents of a logfile are copied instead of renaming, and then the log is merely truncated.

If a process reopens logs for each write (thus it should close the log after that, otherwise the open file handles will stack up unless exhausted to the OS/user limit), then you can omit the owner process notification. But this is a rare case, because this approach jeopardizes the log writer performance when lots of info is constantly written into logs.

drookie
  • 8,625
  • 1
  • 19
  • 29