23

How does logrotate handle open files? Can logrotate rotate files that a process has open?

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444

3 Answers3

19

Looks like I might want the following, but still interested in anyone's expertise on the topic:

copytruncate
              Truncate  the original log file to zero size in place after cre‐
              ating a copy, instead of moving the old log file and  optionally
              creating  a new one. 
Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
  • 2
    It's like doing "cp something.log something.log.1; :> something.log" so the file get empty but the filehandle on it is not break. The problem is the time between the 2 commands, you could loose some line of logs – radius Aug 18 '09 at 15:52
9

For those applications that don't accept the signals from logrotate as Rory described, I use a couple of methods.

  1. Use the copytruncate option
  2. Add a post-rotate statement to restart the service

The decision of which one to use depends on both size of log files and necessity of seamless logs. That's going to be a risk analysis on your own part. However, to give an example, I use a post-rotate restart on certain logs where I actually should be using a copytruncate. However, the files are often several gigs and the copy could take a long enough time that losing a second or two each night was preferable.

Scott Pack
  • 14,717
  • 10
  • 51
  • 83
  • Sorry for beign too late, but i guess i could save some time to others looking for this problem. Take a look to another approach to solve this. https://unix.stackexchange.com/a/122942/189725 – Victor May 12 '17 at 23:18
6

logrotate will send the correct signal to the process to tell it to reopen log files. It works out of the box for things like apache or mysql.

You can see this in the postrotate script for apache. If you have your own daemon, and you want logrotate to work with it, then make sure it listens for a certain signal and reopens log files.

If you can't change the programme, then you could see if the daemon has a way to automatically flush logs. Or add a prerotate that stops the server, and a postrotate that starts it again.

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246