views:

31

answers:

2

I have a daemon writing to a log file that, eventually, fills up the disk. Is there a way for me to periodically limit the size of the log file without stopping the daemon without changing the code in it? SIGHUP kills the daemon.

A: 

If the filename is constant, you can try creating a fifo in place of the log file and read from it into as many separate log iles as you want.

Alex B
This is a good option, but requires writing more code to manage the fifo. Thanks.
arturh
+1  A: 

The usual trick is:

echo -n > /var/log/name.log

That would work provided that your daemon properly open the log file in append mode. Most of them do. (That command simply truncates file size to zero and that doesn't intervene with another process writing to the file in append mode.)

Another option is to check whether your daemon support syslog and activate it. Most Linuxes now are shipped with some log collector which automatically (based on rules, etc) archive syslog files.

Dummy00001
So if a file is open in append mode and another process truncates it, the first process can keep writing to it. Thanks!
arturh
Point of the append mode is that writing operation atomically seeks to the end of the file before the actual write - and writes at the end of the file. If one process truncated the file, the daemon wouldn't even notice that as due to the append mode the OS will automatically seek to the end of the file, now at offset 0.
Dummy00001