Rotate a file that's open and being written at all times

I have an linux application that continually writes logging information into a log file, eg. /var/log/application.log. As the application does not rotate the file automatically, this log file can reach a size of gigabytes in some weeks, so I want to be able to rotate this file properly

My main concern here is that to rotate a file that is opened by the application at all times, I will probably need to:

  1. Move the file to its rotated form /var/log/application.log -> /var/log/application.log.2013-01-28

  2. Create an empty /var/log/application.log. Obs: At this point the application process is still writing to /var/log/application.log.2013-01-28

  3. Change the file descriptor of the application process to point back again to /var/log/application.log

So, am I right? If so, how can i do this? (mainly the changing the file descriptor part)

If i am not, what is the correct way and how to do it?


Solution 1:

Write a logrotate config to use copytruncate

copytruncate
    Truncate the original log file 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 truncating 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.

Solution 2:

Most such applications respond to a signal, such as SIGHUP, and will close and reopen their log files on receipt of the signal. Check your application's documentation for the correct signal to send.