Ubuntu 10.4 Server.
I have a dumb non-interactive legacy service which is running constantly on my server.
It is writing its log to a file with fixed name (/var/log/something.log).
It does not handle any signals to let go of the log file. I need to rotate that log file.
Is there a way to do this properly without changing the application and without losing any data in the log?
Log to a FIFO, then run a daemon that connects to the other side of the FIFO and has signal handlers that allow you to rotate the log.
Ignacio's answer intrigued me so I did some research and came up with the Perl script below. If your service will write to a named pipe it should work and be usable with logrotate.
For it to work you need to make your logfile into a named pipe. Rename the existing file then
and to edit the 3 filenames to meet your requirements. Run your service then this daemon which should read the named pipe and write it to a new logfile.
If you rename
/var/log/somethingrotateable.log
then send a HUP to the daemon it will spawn itself and create a newsomethingrotateable.log
to write to. If using logrotate apostrotate
script ofkill -HUP 'cat /var/run/yourpidfile.pid'
Send SIGSTOP to the process, copy the log to another name, truncate the log, send SIGCONT to the process, perhaps like this:
You could also have logrotate do the magic for you with carefully crafted pre and post rotation scripts and the copytruncate option, like so:
You could try to let the application log to a named pipe and have some program (for example syslog-ng) that supports proper log rotation mechanisms read the log entries and log them to a file.