s6
Software
skarnet.org

The s6-background-watch program

s6-background-watch spawns an auto-backgrounding daemon that writes a pidfile. It reads the pidfile then transmits the signals it receives to the corresponding process. It dies when the corresponding process dies, with an approximation of its exit code.

s6-background-watch is meant as a workaround to auto-backgrounding daemons. (Not a solution, because the solution is to fix the daemon so it runs without backgrounding itself!) It should be run on the command line of a run script, right before the invocation of the auto-backgrounding daemon.

s6-background-watch only works on Linux (or any systems implementing prctl with PR_SET_CHILD_SUBREAPER) and the BSDs (or any system implementing procctl with PROC_REAP_ACQUIRE or kevent with EVFILT_PROC and NOTE_EXIT fflag). Most modern systems will implement one of these.

Interface

     s6-background-watch [ -t timeout ] [ -d notif ] pidfile prog...

Options

-t timeout, --timeout-ready=timeout
If the parent daemon has not exited after timeout milliseconds, kill it and exit. By default, there is no timeout, s6-background-watch will wait as long as needed.
-d notif, --notification-fd=notif
Write a newline to file descriptor notif when the parent has exited 0 and s6-background-watch has successfully read pidfile. This is useful when the daemon follows the convention that the child is ready when the parent exits: use the -d option in conjunction with the notification-fd file, and s6-supervise will accurately report the daemon's readiness.

Exit codes

100
Bad usage.
111
System call failed. This is generally a temporary error, or indicates a problem with the underlying system.
112
Functionality not supported. You cannot run s6-background-watch on this OS, because it lacks the necessary features.
128+n
The parent daemon was killed with signal n. In particular, 137 can happen if the -t option has been given and the parent daemon times out: s6-background-watch kills it with SIGKILL.
n
The child daemon exited with code n.

Notes

s6-background-watch acts as a proxy between s6-supervise and the forking daemon. It stays in the foreground, keeping s6-supervise happy; and it keeps track of the daemon as best as it can, transmitting it signals. It is a better iteration of s6-fghack, which is obsolescent. However, it is not perfect, because it cannot be: