Pipelog allows for rotating or clearing the log of a running process by piping it through an intermediate which responds to external signals. For example:

sh> myapp 2>&1 | pipelog mylog.txt &
[pipelog 5334] Using 'mylog.txt' as a log file.

Starts 'myapp' with its standard output and error streams fed to pipelog and forks into the background. Without such an intermediate, replacing, moving or deleting the log would make it impossible to resume accumulating output because the process will continue to use a filehandle to an unlinked inode; some applications (such as syslogd) implement signal handling themselves so that another utility (such as logrotate) can be used to move their log(s), and then signal the application to re-open — but this is often not the case.

To rotate or clear the log via the pipelog:

sh> kill -s USR1 5334
sh> kill -s USR2 5334

The first signal (SIGUSR1) will rotate the file to 'mylog.txt.1' (similarly rotating any previous logs present) and begin a new 'mylog.txt'. The second signal (SIGUSR2) will discard the current log and begin a new one. '5334' is the PID of the pipelog process from the example, as reported at invocation above. There are several optional means of recording the PID so that a service could be, e.g., effectively daemonized with the log autorotated in short script:


rm -f $pidfile
setsid myapp </dev/zero 2>&1 | pipelog myapp.log -p $pidfile -f &> pipelog.log &
# Avoid race conditions with the forked process and the creation of $pidfile.
while [ ! -e $pidfile ]; do sleep 5; done;
plog -p $pipelog_pid -i 240   # Optional, see here.
sleep 12h
while [ "$(ps -o cmd= -p $pipelog_pid)" == "pipelog" ]; do
	kill -USR1 $pipelog_pid
	sleep 12h

When exiting normally (because input from the logged process terminated), pipelog will remove the pid file, hence its existence can be tested. However, the condition of the while loop ensures that the pipelog was not killed some other way — in which case the PID may be in use by a new process, and sending SIGUSR1 to a mystery client is not desirable. Note that it makes more sense to do all this via cron in a separate script, using 'if' instead of 'while'. The '-f' switch sends pipelog's own output to standard error (the default is syslog) so that it can be logged locally.

Pipelog can also execute a user defined process in between opening a new log and starting to write to it, while buffering input. More information can be found in the man page.


This is the first release, April 2014 — via sourceforge: pipelog-0.1.tar.gz