xref: /src/usr.sbin/daemon/daemon.8 (revision a3b90a1f008365d9f62773998f89f9c872e2bed5)
1.\" Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\"    notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\"    notice, this list of conditions and the following disclaimer in the
10.\"    documentation and/or other materials provided with the distribution.
11.\" 3. Berkeley Software Design Inc's name may not be used to endorse or
12.\"    promote products derived from this software without specific prior
13.\"    written permission.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.Dd January 28, 2026
28.Dt DAEMON 8
29.Os
30.Sh NAME
31.Nm daemon
32.Nd run detached from the controlling terminal
33.Sh SYNOPSIS
34.Nm
35.Op Fl cfHrS
36.Op Fl p Ar child_pidfile
37.Op Fl P Ar supervisor_pidfile
38.Op Fl t Ar title
39.Op Fl u Ar user
40.Op Fl m Ar output_mask
41.Op Fl o Ar output_file
42.Op Fl s Ar syslog_priority
43.Op Fl T Ar syslog_tag
44.Op Fl l Ar syslog_facility
45.Op Fl R Ar restart_delay_seconds
46.Op Fl C Ar restart_count
47.Ar command arguments ...
48.Sh DESCRIPTION
49The
50.Nm
51utility detaches itself from the controlling terminal and
52executes the program specified by its arguments.
53Privileges may be lowered to the specified user.
54The output of the daemonized process may be redirected to syslog and to a
55log file.
56.Pp
57The options are as follows:
58.Bl -tag -width indent
59.It Fl C , Fl -restart-count Ar restart_count
60Restart the process at most
61.Ar restart_count
62times.
63When zero is specified,
64.Nm
65will exit.
66The maximum restart count is
67.Cm 128 .
68This option is used together with option
69.Fl -restart .
70.It Fl c , Fl -change-dir
71Change the current working directory to the root
72.Pq Dq Pa / .
73.It Fl f , Fl -close-fds
74Redirect standard input, standard output and standard error to
75.Pa /dev/null .
76When this option is used together with any of the options related to file
77or syslog output, the standard file descriptors are first redirected to
78.Pa /dev/null ,
79then stdout and/or stderr is redirected to a file or to syslog as
80specified by the other options.
81.It Fl H , Fl -sighup
82Close
83.Pa output_file
84and re-open it when signal
85.Dv SIGHUP
86is received, for interoperability with
87.Xr newsyslog 8
88and similar log rotation / archival mechanisms.
89If
90.Fl -output-file
91is not specified, this flag is ignored.
92.It Fl l , Fl -syslog-facility Ar syslog_facility
93These facilities are accepted:
94.Cm auth , authpriv , console , cron , daemon ,
95.Cm ftp , kern , lpr , mail , news , ntp , security ,
96.Cm syslog , user , uucp ,
97and
98local facilities from
99.Cm local0
100to
101.Cm local7 .
102The default is
103.Cm daemon .
104.It Fl m , Fl -output-mask Ar output_mask
105Redirect output from the child process stdout
106.Pq Cm 1 ,
107stderr
108.Pq Cm 2 ,
109or both
110.Pq Cm 3 .
111This value specifies what is sent to syslog and the log file.
112The default is
113.Cm 3 .
114.It Fl M , Fl -output-file-mode Ar mode
115Specify the file
116.Ar mode
117to use when creating the
118.Pa output_file .
119The mode is interpreted using
120.Xr chmod 1
121style semantics.
122This option is useful when the daemonized process or external log
123collectors require group or world access to the output file.
124The default is
125.Cm 0600 .
126.It Fl o , Fl -output-file Ar output_file
127Append output from the daemonized process to
128.Pa output_file .
129If the file does not exist, it is created with the default file
130.Ar mode .
131When this option is used together with options
132.Fl -change-dir
133and
134.Fl -sighup
135the absolute path needs to be provided to ensure
136.Nm
137can re-open the file after a
138.Dv SIGHUP .
139.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile
140Write the ID of the
141.Nm
142process into the
143.Ar supervisor_pidfile
144using the
145.Xr pidfile 3
146functionality.
147The program is executed in a spawned child process while the
148.Nm
149waits until it terminates to keep the
150.Ar supervisor_pidfile
151locked and removes it after the process exits.
152The
153.Ar supervisor_pidfile
154owner is the user who runs the
155.Nm
156regardless of whether the
157.Fl -user
158option is used or not.
159.It Fl p , Fl -child-pidfile Ar child_pidfile
160Write the ID of the created process into the
161.Ar child_pidfile
162using the
163.Xr pidfile 3
164functionality.
165The program is executed in a spawned child process while the
166.Nm
167waits until it terminates to keep the
168.Ar child_pidfile
169locked and removes it after the process exits.
170The
171.Ar child_pidfile
172owner is the user who runs the
173.Nm
174regardless of whether the
175.Fl -user
176option is used or not.
177.It Fl R , Fl -restart-delay Ar restart_delay_seconds
178Supervise and restart the program after the specified delay
179if it has been terminated.
180Valid values are 1-31536000 (up to 1 year).
181.It Fl r , Fl -restart
182Supervise and restart the program after a one-second delay if it has
183been terminated.
184.It Fl S , Fl -syslog
185Enable syslog output.
186This is implicitly applied if other syslog parameters are provided.
187The default values are daemon, notice, and daemon for facility, priority, and
188tag, respectively.
189.It Fl s , Fl -syslog-priority Ar syslog_priority
190These priorities are accepted:
191.Cm emerg , alert , crit , err , warning ,
192.Cm notice , info ,
193and
194.Cm debug .
195The default is
196.Cm notice .
197.It Fl T , Fl -syslog-tag Ar syslog_tag
198Set the tag which is appended to all syslog messages.
199The default is
200.Cm daemon .
201.It Fl t , Fl -title Ar title
202Set the title for the daemon process.
203The default is the daemonized invocation.
204.It Fl u , Fl -user Ar user
205Login name of the user to execute the program under.
206Environment variables
207.Ev HOME , USER ,
208and
209.Ev SHELL
210are set accordingly.
211Requires adequate superuser privileges.
212.El
213.Pp
214If any of the options
215.Fl -child-pidfile ,
216.Fl -output-mask ,
217.Fl -restart ,
218.Fl -restart-delay ,
219.Fl -supervisor-pidfile ,
220.Fl -syslog ,
221.Fl -syslog-facility
222.Fl -syslog-priority ,
223.Fl -syslog-tag ,
224or
225.Fl -output ,
226are specified, the program is executed in a spawned child process.
227The
228.Nm
229waits until it terminates to keep the pid file(s) locked and removes them
230after the process exits or restarts the program.
231In this case if the monitoring
232.Nm
233receives software termination signal
234.Pq Dv SIGTERM
235it forwards it to the
236spawned process.
237Normally it will cause the child to exit, remove the pidfile(s)
238and then terminate.
239.Pp
240If neither file or syslog output are selected, all output is redirected to the
241.Nm
242process and written to stdout.
243The
244.Fl -close-fds
245option may be used to suppress the stdout output completely.
246.Pp
247The
248.Fl -supervisor-pidfile
249option is useful combined with the
250.Fl -restart
251option as
252.Ar supervisor_pidfile
253contains the ID of the supervisor
254not the child.
255This is especially important if you use
256.Fl -restart
257in an rc script as the
258.Fl -child-pidfile
259option will give you the child's ID to signal when you attempt to
260stop the service, causing
261.Nm
262to restart the child.
263.Sh EXIT STATUS
264The
265.Nm
266utility exits 1 if an error is returned by the
267.Xr daemon 3
268library routine, 2 if
269.Ar child_pidfile
270or
271.Ar supervisor_pidfile
272is requested, but cannot be opened, 3 if process is already running (pidfile
273exists and is locked), 4 if
274.Ar syslog_priority
275is not accepted, 5 if
276.Ar syslog_facility
277is not accepted, 6 if
278.Ar output_mask
279is not within the accepted range, 7 if
280.Ar output_file
281cannot be opened for appending, and otherwise 0.
282.Sh DIAGNOSTICS
283If the command cannot be executed, an error message is printed to
284standard error.
285The exact behavior depends on the logging parameters and the
286.Fl -close-fds
287flag.
288.Sh SEE ALSO
289.Xr nohup 1 ,
290.Xr setregid 2 ,
291.Xr setreuid 2 ,
292.Xr daemon 3 ,
293.Xr exec 3 ,
294.Xr pidfile 3 ,
295.Xr termios 4 ,
296.Xr tty 4
297.Sh HISTORY
298The
299.Nm
300utility first appeared in
301.Fx 4.7 .
302