daemon - run a process detached from the current session
daemon [-cf] [-p pidfile] [-u user[:group]] utility [arguments]
Daemon's positional parameter utility is the path execve(2)'d to create
the new detached process. The new background process is the session
leader of a new session, see setsid(2). Thus the HUP signal delivered
at the end of the current session is not delivered to the task.
The task may still be ended via a signal. To that end, the new process
ID may be recorded in pidfile, thus allowing signal deliver to termi-
nate the task with an explict kill(1). The process may also be located
via pgrep(1), or signaled via pkill(1).
If the program is called as daemon then no options are forced.
Change the current working directory to the root of the filesys-
tem before executing utility.
Redirect standard input, output and error to /dev/null.
Print only the standard help message.
Write new process ID to this file, delete on failure. Don't
start the utility if the process ID in pidfile is presently run-
ning. Specification of dash (-) as the pidfile is an extension
of the program which builds a default name from the basename of
the utility under /var/run.
Drop credentials to this login before utility execution. Any
group specified is set by a call to setgid(2) after the call to
initgroups(3). The original credentials must allow the
setuid(2) to succeed (viz. usually only the superuser can do
this). The empty user is mapped to "nobody".
Show only version information and exit.
daemon sleep 1000
Push a sleep(1) command in its own session the background, but
don't track the process ID.
daemon sleeep 1000
The misspelling of sleep(1) produces a useful error to stderr:
daemon: execvp: sleeep: No such file or directory
This is not true for nohup(1) which puts the error in nohup.out.
daemon -u nobody /usr/local/libexec/unixstats -p113
Run the PEG data collector unixstats as login nobody. Of course
the change of user must be done as the superuser.
daemon -u nobody:sample /usr/local/libexec/unixstats -p113
Same as the previous, but run the collector as login nobody in
daemon -c -p /var/run/dealer.pid -u house /usr/local/libexec/dealer
Run the dealer program as login house with a pid file in
/var/run/dealer.pid to prevent multiple instances.
daemon -c -p - -u house /usr/local/libexec/dealer
Same as the above command, but let daemon build pidfile.
pkill -QUIT -F /var/run/dealer.pid -U house
Safely terminate any running dealer process. By including both
the pidfile and the login name we limit pkill's actions enough
that we are unlikely to shoot an innocent (unrelated) process.
See the informative version output, which includes the printf(3)
format used to construct default pidfile names.
The addition of a group is non-portable, so it is not listed in the on-
line help output.
The flock(2) system call the BSD version of this program used to lock
pidfile is, sadly, not portable (anymore). This version only locks the
pidfile for updates.
A pidfile left-over from a previous system boot may contain the process
ID of an unrelated process, which will keep daemon from starting a new
instance. Always clean /var/run at system boot to prevent this bug.
A pidfile with a non-numeric string in it prevents daemon from ever
starting a process, or updating the file. This is also a feature. For
example I use the string "#locked" to denote an explicitly locked
facility. A file with only white-space in it is still going to block
the process, so check for just a newline in it.
The exit codes are not from sysexits(3). They are 1 for for failed
system calls, 2 for failure to build or update the pidfile, and 3 when
the process in the pidfile is running or the file is removed while we
are updating it.
KS Braunsdorf with liberal consultation from the FreeBSD version.
NonPlayer Character Guild
op at ksb dot npc guild dot orgspam-me-not
sh(1), nohup(1), op(1l), unixstats(7l), pkill(1)