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 -E name=value
70 Add an environment variable to the environment the daemon will
71 see. The parameter must be of the form name=value. This parame‐
72 ter may be specified multiple times.
73
74 -o stdout
75 Redirect standard output to the specified file, instead of
76 "/dev/null".
77
78 Warning: Be careful where you redirect the output! The file sys‐
79 tem containing the open file cannot be unmounted as long as the
80 file is open. For best results, make sure that this output file
81 is on the same file system as the daemon's working directory.
82 (See the -c option.)
83
84 -p pidfile
85 Causes daemonize to write the numeric process ID (PID) of the
86 running daemon to the specified file. This option is useful when
87 the program being daemonized doesn't create its own PID file.
88
89 -l lockfile
90 Single-instance checking. Causes daemonize to ensure that no
91 more than one instance of the daemon is running by placing an
92 exclusive lock on given lockfile. If another process already has
93 a lock on the lockfile, daemonize exits.
94
95 It is possible to use the pidfile as the lock file (e.g., "-p
96 /var/run/foo -l /var/run/foo"), though typical daemons use separate
97 files.
98
99 NOTE: If the executed program decides to close all file descriptors,
100 the single-instance locking will not work, since the lock depends on an
101 open file descriptor. (The operating system kernel removes the lock
102 once the process holding the lock closes the file or exits.) Normal
103 processes that do not daemonize themselves do not usually close all
104 file descriptors.
105
106 -u user
107 Run the program as the specified user. This option only works if
108 daemonize is invoked by the superuser. Note: For obvious rea‐
109 sons, it's very dangerous to install daemonize as a setuid-to-
110 root executable. For that reason, daemonize will refuse to run
111 if it detects that it has been installed that way.
112
113 -v Cause daemonize to write verbose messages to standard error,
114 telling what it's doing as it daemonizes the program.
115
117 If the host operating system provides the daemon(3) library routine,
118 daemonize will use it. Otherwise, daemonize uses its own version of
119 daemon(3). This choice is made at compile time. (BSD 4.4-derived oper‐
120 ating systems tend to provide their own daemon(3) routine.)
121
122 FreeBSD 5.0 introduced a daemon(1) command that is similar to, but less
123 functional, than daemonize.
124
126 This program released under a BSD-style license. For more details, con‐
127 sult the LICENSE file accompanying the source distribution, or visit
128 "http://software.clapper.org/daemonize/LICENSE".
129
131 daemon(3), setsid(2), flock(2)
132
133 daemonize Home Page: http://software.clapper.org/daemonize/
134
136 Brian M. Clapper, bmc <at> clapper <dot> org
137
139 Support for the -e and -o options is based on a patch submitted by Tim
140 Starling (tstarling <at> wikimedia <dot> org).
141
142 Support for the -l option is based on a patch submitted by Yakov Lerner
143 (iler.ml <at> gmail <dot> com).
144
145
146
147Unix August 2006 daemonize(1)