$RodHat_/search
Console Tips

daemon(8) on FreeBSD is a supervisor, and you're using it as a backgrounder

Published by

This article may contain Amazon affiliate links. I may earn a commission if you buy through them. Disclosure.

Terminal showing a FreeBSD daemon supervisor restarting a crashed service
AI illustration: RodHat
On this page · 4 sections
  1. The flags that matter
  2. The two pidfiles
  3. Wiring it into rc.d
  4. What it does not do

Most people who know daemon(8) know it as the thing that puts a foreground program in the background. daemon sleep 1000, done, shell returned. That is the least interesting thing it does. It ships in the FreeBSD base system, and with one flag it becomes a small process supervisor that restarts your service when it dies. No extra package, no unit files, no D-Bus.

The flags that matter

daemon -r -R 5 -P /var/run/myapp.supervisor.pid \
       -p /var/run/myapp.pid \
       -o /var/log/myapp.log \
       /usr/local/bin/myapp --foreground
  • -r restarts the program whenever it exits. This is the whole trick.
  • -R secs waits that many seconds before restarting. Without a delay a crashing binary spins as fast as fork allows, so always set one. Check the minimum and default on your release’s man page; I am not going to quote one from memory.
  • -P file writes the pid of the supervisor (the daemon process itself).
  • -p file writes the pid of the child.
  • -o file sends the child’s stdout and stderr to a file. -S with -T tag sends them to syslog instead, with -l and -s for facility and priority.
  • -u user drops to a user before running the child, and -t title sets a readable process title so ps shows something other than a path.

Your program has to stay in the foreground. If it forks and exits on its own, daemon -r sees the exit, restarts it, and you get a pile of orphaned copies fighting over one port. Turn off the program’s own daemonizing option first.

The two pidfiles

This is where people lose an afternoon. -P is the supervisor and -p is the child. If you want to stop the service, you signal the supervisor. Send SIGTERM to the supervisor and it terminates the child and exits. Kill only the child and the supervisor, doing its job, starts a new one 5 seconds later. That is not a bug, it is the feature you asked for, and it is also exactly what “I killed it and it came back” means.

Use -p when you want to signal the real process, for a config reload with SIGHUP, say:

kill -HUP $(cat /var/run/myapp.pid)

Wiring it into rc.d

rc.subr already understands this pattern. The pidfile variable is what service myapp stop signals, so point it at the supervisor:

#!/bin/sh
# PROVIDE: myapp
# REQUIRE: NETWORKING
# KEYWORD: shutdown

. /etc/rc.subr

name=myapp
rcvar=myapp_enable
pidfile=/var/run/${name}.supervisor.pid
child_pidfile=/var/run/${name}.pid
command=/usr/sbin/daemon
procname=daemon
command_args="-r -R 5 -P ${pidfile} -p ${child_pidfile} -T ${name} -S /usr/local/bin/myapp --foreground"

load_rc_config $name
: ${myapp_enable:=NO}
run_rc_command "$1"

Then sysrc myapp_enable=YES && service myapp start. Getting procname right matters. rc.subr verifies the pid it finds really belongs to a process of that name, and the supervisor is daemon, not myapp. Get it wrong and service myapp status lies to you, which is a nice way to waste an hour.

What it does not do

Be honest about the limits, because the first outage will find them.

  • No backoff. The delay is fixed. A service that crashes on startup because the database is gone will restart every 5 seconds all night. Pair it with something that tells you, because the log noise is the only alarm you get.
  • No health checks. It knows the process exited. It does not know the process is hung, deadlocked, or answering every request with a 500. A wedged process is still alive as far as daemon is concerned, and reading a wedged process without a dashboard is how you find out.
  • No dependencies or sockets. rc.d ordering handles start order once at boot. Nothing re-checks it after a restart.
  • Restart loops are evidence, so keep it. Log to syslog or a file, not /dev/null. The restart-loop evidence post covers what to capture before the next crash overwrites it.

If you need real supervision with per-service control, runit or s6 from ports do it properly, and I walked through the pattern in supervision without systemd. But for one or two long-running daemons on a jail or a small box, daemon -r plus a sensible -R is about 30 seconds of work and nothing new to learn at 3 a.m.

More of the same kind of thing lives in the FreeBSD and OpenBSD hub. Systemd users will tell you this is primitive. It is. It is also readable in one screen, and the screen never needed an update.