xref: /freebsd/usr.sbin/daemon/daemon.8 (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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 April 25, 2024
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 o , Fl -output-file Ar output_file
115Append output from the daemonized process to
116.Pa output_file .
117If the file does not exist, it is created with permissions 0600.
118When this option is used together with options
119.Fl -change-dir
120and
121.Fl -sighup
122the absolute path needs to be provided to ensure
123.Nm
124can re-open the file after a
125.Dv SIGHUP .
126.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile
127Write the ID of the
128.Nm
129process into the
130.Ar supervisor_pidfile
131using the
132.Xr pidfile 3
133functionality.
134The program is executed in a spawned child process while the
135.Nm
136waits until it terminates to keep the
137.Ar supervisor_pidfile
138locked and removes it after the process exits.
139The
140.Ar supervisor_pidfile
141owner is the user who runs the
142.Nm
143regardless of whether the
144.Fl -user
145option is used or not.
146.It Fl p , Fl -child-pidfile Ar child_pidfile
147Write the ID of the created process into the
148.Ar child_pidfile
149using the
150.Xr pidfile 3
151functionality.
152The program is executed in a spawned child process while the
153.Nm
154waits until it terminates to keep the
155.Ar child_pidfile
156locked and removes it after the process exits.
157The
158.Ar child_pidfile
159owner is the user who runs the
160.Nm
161regardless of whether the
162.Fl -user
163option is used or not.
164.It Fl R , Fl -restart-delay Ar restart_delay_seconds
165Supervise and restart the program after the specified delay
166if it has been terminated.
167Valid values are 1-31536000 (up to 1 year).
168.It Fl r , Fl -restart
169Supervise and restart the program after a one-second delay if it has
170been terminated.
171.It Fl S , Fl -syslog
172Enable syslog output.
173This is implicitly applied if other syslog parameters are provided.
174The default values are daemon, notice, and daemon for facility, priority, and
175tag, respectively.
176.It Fl s , Fl -syslog-priority Ar syslog_priority
177These priorities are accepted:
178.Cm emerg , alert , crit , err , warning ,
179.Cm notice , info ,
180and
181.Cm debug .
182The default is
183.Cm notice .
184.It Fl T , Fl -syslog-tag Ar syslog_tag
185Set the tag which is appended to all syslog messages.
186The default is
187.Cm daemon .
188.It Fl t , Fl -title Ar title
189Set the title for the daemon process.
190The default is the daemonized invocation.
191.It Fl u , Fl -user Ar user
192Login name of the user to execute the program under.
193Environment variables
194.Ev HOME , USER ,
195and
196.Ev SHELL
197are set accordingly.
198Requires adequate superuser privileges.
199.El
200.Pp
201If any of the options
202.Fl -child-pidfile ,
203.Fl -output-mask ,
204.Fl -restart ,
205.Fl -restart-delay ,
206.Fl -supervisor-pidfile ,
207.Fl -syslog ,
208.Fl -syslog-facility
209.Fl -syslog-priority ,
210.Fl -syslog-tag ,
211or
212.Fl -output ,
213are specified, the program is executed in a spawned child process.
214The
215.Nm
216waits until it terminates to keep the pid file(s) locked and removes them
217after the process exits or restarts the program.
218In this case if the monitoring
219.Nm
220receives software termination signal
221.Pq Dv SIGTERM
222it forwards it to the
223spawned process.
224Normally it will cause the child to exit, remove the pidfile(s)
225and then terminate.
226.Pp
227If neither file or syslog output are selected, all output is redirected to the
228.Nm
229process and written to stdout.
230The
231.Fl -close-fds
232option may be used to suppress the stdout output completely.
233.Pp
234The
235.Fl -supervisor-pidfile
236option is useful combined with the
237.Fl -restart
238option as
239.Ar supervisor_pidfile
240contains the ID of the supervisor
241not the child.
242This is especially important if you use
243.Fl -restart
244in an rc script as the
245.Fl -child-pidfile
246option will give you the child's ID to signal when you attempt to
247stop the service, causing
248.Nm
249to restart the child.
250.Sh EXIT STATUS
251The
252.Nm
253utility exits 1 if an error is returned by the
254.Xr daemon 3
255library routine, 2 if
256.Ar child_pidfile
257or
258.Ar supervisor_pidfile
259is requested, but cannot be opened, 3 if process is already running (pidfile
260exists and is locked), 4 if
261.Ar syslog_priority
262is not accepted, 5 if
263.Ar syslog_facility
264is not accepted, 6 if
265.Ar output_mask
266is not within the accepted range, 7 if
267.Ar output_file
268cannot be opened for appending, and otherwise 0.
269.Sh DIAGNOSTICS
270If the command cannot be executed, an error message is printed to
271standard error.
272The exact behavior depends on the logging parameters and the
273.Fl -close-fds
274flag.
275.Sh SEE ALSO
276.Xr nohup 1 ,
277.Xr setregid 2 ,
278.Xr setreuid 2 ,
279.Xr daemon 3 ,
280.Xr exec 3 ,
281.Xr pidfile 3 ,
282.Xr termios 4 ,
283.Xr tty 4
284.Sh HISTORY
285The
286.Nm
287utility first appeared in
288.Fx 4.7 .
289