Kathy.Pu
Kathy.Pu

Reputation: 97

How does logrotate work when there are two process use the same file?

For example:

  1. Program A is writing log to file "test.log".

  2. If logrotate runs, it will rename "test.log" to "test.log.1" first, and then create a new file "test.log".

After step 2, program A does not report any error, but the A's log does not appear in the new file "test.log".

The questions are:

  1. Where is the data that A write to file after step 2 ?

  2. How can logrotate rename and create new file when another process is writing to the file? (Is any point that I miss about logrotate?)

Thanks!

Upvotes: 2

Views: 972

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

This is very tightly related to how POSIX filesystems work. When you rename a file, it's only the name of the file that is changed, the physical file on the disk will not change. Also, once a file is opened, the process using the file only have a link (through many layers) to the physical file on the disk, the name is only used when opening the file.

That means the program A will still write to the same file, which now has the new name (i.e. test.log.1 in your example).

A common solution to this problem is to have the log rotation program send a signal (e.g. SIGHUP or SIGUSR1 or similar) to the process. The process will detect this signal and then reopen the logging to use the new file.

Upvotes: 4

Related Questions