Score:0

How does logrotate work?

mx flag
TS_

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?

in flag
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)
Score:0
za flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.