1daemonize(1) User Manuals daemonize(1)
2
3
4
6 daemonize - run a program as a Unix daemon
7
9 daemonize [-a] [-c directory] [-e stderr] [-o stdout] [-p pidfile] [-l
10 lockfile] [-u user] [-v] path [arg] ...
11
13 daemonize runs a command as a Unix daemon. As defined in W. Richard
14 Stevens' 1990 book, Unix Network Programming (Addison-Wesley, 1990), a
15 daemon is “a process that executes `in the background' (i.e., without
16 an associated terminal or login shell) either waiting for some event to
17 occur, or waiting to perform some specified task on a periodic basis.”
18 Upon startup, a typical daemon program will:
19
20 o Close all open file descriptors (especially standard input, standard
21 output and standard error)
22
23 o Change its working directory to the root filesystem, to ensure that
24 it doesn't tie up another filesystem and prevent it from being
25 unmounted
26
27 o Reset its umask value
28
29 o Run in the background (i.e., fork)
30
31 o Disassociate from its process group (usually a shell), to insulate
32 itself from signals (such as HUP) sent to the process group
33
34 o Ignore all terminal I/O signals
35
36 o Disassociate from the control terminal (and take steps not to reac‐
37 quire one)
38
39 o Handle any SIGCLD signals
40
41 Most programs that are designed to be run as daemons do that work for
42 themselves. However, you'll occasionally run across one that does not.
43 When you must run a daemon program that does not properly make itself
44 into a true Unix daemon, you can use daemonize to force it to run as a
45 true daemon.
46
48 -a Append to the output files, rather than overwriting them (which
49 is the default). Only applicable if -e and/or -o are specified.
50
51 -c directory
52 Specifies the directory to which to change before running the
53 program. Defaults to "/". The choice for this option is impor‐
54 tant. The file system containing the daemon's working directory
55 cannot be unmounted while the daemon is running. That's why most
56 daemons are careful to use a working directory on the root file
57 system.
58
59 -e stderr
60 Redirect standard error to the specified file, instead of
61 "/dev/null".
62
63 Warning: Be careful where you redirect the output! The file sys‐
64 tem containing the open file cannot be unmounted as long as the
65 file is open. For best results, make sure that this output file
66 is on the same file system as the daemon's working directory.
67 (See the -c option.)
68
69 -o stdout
70 Redirect standard output to the specified file, instead of
71 "/dev/null".
72
73 Warning: Be careful where you redirect the output! The file sys‐
74 tem containing the open file cannot be unmounted as long as the
75 file is open. For best results, make sure that this output file
76 is on the same file system as the daemon's working directory.
77 (See the -c option.)
78
79 -p pidfile
80 Causes daemonize to write the numeric process ID (PID) of the
81 running daemon to the specified file. This option is useful when
82 the program being daemonized doesn't create its own PID file.
83
84 -l lockfile
85 Single-instance checking. Causes daemonize to ensure that no
86 more than one instance of the daemon is running by placing an
87 exclusive lock on given lockfile. If another process already has
88 a lock on the lockfile, daemonize exits.
89
90 It is possible to use the pidfile as the lock file (e.g., "-p
91 /var/run/foo -l /var/run/foo"), though typical daemons use separate
92 files.
93
94 NOTE: If the executed program decides to close all file descriptors,
95 the single-instance locking will not work, since the lock depends on an
96 open file descriptor. (The operating system kernel removes the lock
97 once the process holding the lock closes the file or exits.) Normal
98 processes that do not daemonize themselves do not usually close all
99 file descriptors.
100
101 -u user
102 Run the program as the specified user. This option only works if
103 daemonize is invoked by the superuser. Note: For obvious rea‐
104 sons, it's very dangerous to install daemonize as a setuid-to-
105 root executable. For that reason, daemonize will refuse to run
106 if it detects that it has been installed that way.
107
108 -v Cause daemonize to write verbose messages to standard error,
109 telling what it's doing as it daemonizes the program.
110
112 If the host operating system provides the daemon(3) library routine,
113 daemonize will use it. Otherwise, daemonize uses its own version of
114 daemon(3). This choice is made at compile time. (BSD 4.4-derived oper‐
115 ating systems tend to provide their own daemon(3) routine.)
116
117 FreeBSD 5.0 introduced a daemon(1) command that is similar to, but less
118 functional, than daemonize.
119
121 This program released under a BSD-style license. For more details, con‐
122 sult the LICENSE file accompanying the source distribution, or visit
123 "http://www.clapper.org/software/daemonize/LICENSE".
124
126 daemon(3), setsid(2), flock(2)
127
128 daemonize Home Page: http://www.clapper.org/software/daemonize/
129
131 Brian M. Clapper, bmc <at> clapper <dot> org
132
134 Support for the -e and -o options is based on a patch submitted by Tim
135 Starling (tstarling <at> wikimedia <dot> org).
136
137 Support for the -l option is based on a patch submitted by Yakov Lerner
138 (iler.ml <at> gmail <dot> com).
139
140
141
142Unix August 2006 daemonize(1)