Score:0

Logrotate not writing to new .log file

jp flag

I have 4 custom systemd services that write to custom logs in /var/log/. I set up a logrotate config file, but it is not writing to the newest log. It is still writing to the old .log.1 file. The new ones are created, just no data.

Here is my logrotate config:

/var/log/dealer*.log {
        daily
        missingok
        rotate 7
        delaycompress
        notifempty
        su root root
        postrotate
        /bin/systemctl reload dealer*.service > /dev/null 2>/dev/null || true
        endscript
        create 0644 root root

}

The newest log files are empty:

-rw-r--r--  1 root      root               0 Jul 12 11:39 dealerF2f.log
-rw-r--r--  1 root      root               0 Jul 12 11:39 dealerOutbound.log
-rw-r--r--  1 root      root               0 Jul 12 11:39 dealerTimeout.log
-rw-r--r--  1 root      root               0 Jul 12 11:39 dealerWeb.log

It is still writing to the .log.1:

-rw-r--r--  1 root      root             36M Jul 12 12:52 dealerOutbound.log.1
-rw-r--r--  1 root      root            9.2M Jul 12 12:52 dealerF2f.log.1
-rw-r--r--  1 root      root             19M Jul 12 12:52 dealerWeb.log.1
-rw-r--r--  1 root      root            171M Jul 12 12:52 dealerTimeout.log.1
Score:0
it flag

Typically I see something like this when a process still has the file open - it will continue to write to the old file until the process is restarted.

My bet is that if you do a restart instead of a reload it will work as expected.

If you cannot restart the process completely look into the copytruncate option instead of the postrotate script.

copytruncate Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and trun‐ cating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

DevOpsSauce avatar
jp flag
I'm trying to avoid a full restart since my `ExecStopPost` calls a bash script that sends an email, however, using copytruncate and removing the postrotate section did what I needed. It created the new log, and began writing to it correctly. Thank you very much.
Ginnungagap avatar
gu flag
`copytruncate` is a last resort as its design is flawed (because it's the best that can be done given the requirements). The services really should be capable of supporting log file rotation properly.
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.