xref: /freebsd/usr.sbin/daemon/daemon.8 (revision d59c7ea2701fe7b73b32eef49a7c712ef38de5a0)
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 August 13, 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 cfHrSx
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.It Fl x , Fl -execute-only
213Do not supervise the child process when using
214.Fl p .
215This option is useful to run the given program as another user when
216.Xr su 1
217can't be used but a file holding the pid is needed.
218The
219.Ar child_pidfile
220will not be locked and the contents may be silently truncated by
221a concurrent daemon(8) invocation.
222.El
223.Pp
224If any of the options
225.Fl -child-pidfile
226(without
227.Fl -execute-only
228),
229.Fl -output-mask ,
230.Fl -restart ,
231.Fl -restart-delay ,
232.Fl -supervisor-pidfile ,
233.Fl -syslog ,
234.Fl -syslog-facility
235.Fl -syslog-priority ,
236.Fl -syslog-tag ,
237or
238.Fl -output ,
239are specified, the program is executed in a spawned child process.
240The
241.Nm
242waits until it terminates to keep the pid file(s) locked and removes them
243after the process exits or restarts the program.
244In this case if the monitoring
245.Nm
246receives software termination signal
247.Pq Dv SIGTERM
248it forwards it to the
249spawned process.
250Normally it will cause the child to exit, remove the pidfile(s)
251and then terminate.
252.Pp
253If neither file or syslog output are selected, all output is redirected to the
254.Nm
255process and written to stdout.
256The
257.Fl -close-fds
258option may be used to suppress the stdout output completely.
259.Pp
260The
261.Fl -supervisor-pidfile
262option is useful combined with the
263.Fl -restart
264option as
265.Ar supervisor_pidfile
266contains the ID of the supervisor
267not the child.
268This is especially important if you use
269.Fl -restart
270in an rc script as the
271.Fl -child-pidfile
272option will give you the child's ID to signal when you attempt to
273stop the service, causing
274.Nm
275to restart the child.
276.Sh EXIT STATUS
277The
278.Nm
279utility exits 1 if an error is returned by the
280.Xr daemon 3
281library routine, 2 if
282.Ar child_pidfile
283or
284.Ar supervisor_pidfile
285is requested, but cannot be opened, 3 if process is already running (pidfile
286exists and is locked), 4 if
287.Ar syslog_priority
288is not accepted, 5 if
289.Ar syslog_facility
290is not accepted, 6 if
291.Ar output_mask
292is not within the accepted range, 7 if
293.Ar output_file
294cannot be opened for appending, and otherwise 0.
295.Sh DIAGNOSTICS
296If the command cannot be executed, an error message is printed to
297standard error.
298The exact behavior depends on the logging parameters and the
299.Fl -close-fds
300flag.
301.Sh SEE ALSO
302.Xr nohup 1 ,
303.Xr setregid 2 ,
304.Xr setreuid 2 ,
305.Xr daemon 3 ,
306.Xr exec 3 ,
307.Xr pidfile 3 ,
308.Xr termios 4 ,
309.Xr tty 4
310.Sh HISTORY
311The
312.Nm
313utility first appeared in
314.Fx 4.7 .
315