Linux – Is it possible to prune log files opened by daemons without closing files?

Is it possible to prune log files opened by daemons without closing files?… here is a solution to the problem.

Is it possible to prune log files opened by daemons without closing files?

I have a daemon writing to a log file that ends up filling up the disk. Is there a way to periodically limit the size of the log file without changing the code in it while stopping the daemon? SIGHUP kills the daemon.

Solution

The usual trick is:

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

If your daemon opens the log file correctly in append mode, that will work. Most of them do. (The command simply truncates the file size to zero and does not interfere with another process that writes the file in append mode.) )

Another option is to check if your daemon supports syslogging and activate it. Most Linux now ships with some log collector that automatically (rule-based, etc.) archives system log files.

Related Problems and Solutions