xref: /freebsd/usr.sbin/daemon/daemon.8 (revision 069ac18495ad8fde2748bc94b0f80a50250bb01d)
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 December 27, 2023
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.Ar command arguments ...
47.Sh DESCRIPTION
48The
49.Nm
50utility detaches itself from the controlling terminal and
51executes the program specified by its arguments.
52Privileges may be lowered to the specified user.
53The output of the daemonized process may be redirected to syslog and to a
54log file.
55.Pp
56The options are as follows:
57.Bl -tag -width indent
58.It Fl c , Fl -change-dir
59Change the current working directory to the root
60.Pq Dq Pa / .
61.It Fl f , Fl -close-fds
62Redirect standard input, standard output and standard error to
63.Pa /dev/null .
64When this option is used together with any of the options related to file
65or syslog output, the standard file descriptors are first redirected to
66.Pa /dev/null ,
67then stdout and/or stderr is redirected to a file or to syslog as
68specified by the other options.
69.It Fl H , Fl -sighup
70Close
71.Pa output_file
72and re-open it when signal
73.Dv SIGHUP
74is received, for interoperability with
75.Xr newsyslog 1
76and similar log rotation / archival mechanisms.  If
77.Fl -output-file
78is not specified, this flag is ignored.
79.It Fl l , Fl -syslog-facility Ar syslog_facility
80These facilities are accepted:
81.Cm auth , authpriv , console , cron , daemon ,
82.Cm ftp , kern , lpr , mail , news , ntp , security ,
83.Cm syslog , user , uucp ,
84and
85local facilities from
86.Cm local0
87to
88.Cm local7 .
89The default is
90.Cm daemon .
91.It Fl m , Fl -output-mask Ar output_mask
92Redirect output from the child process stdout
93.Pq Cm 1 ,
94stderr
95.Pq Cm 2 ,
96or both
97.Pq Cm 3 .
98This value specifies what is sent to syslog and the log file.
99The default is
100.Cm 3 .
101.It Fl o , Fl -output-file Ar output_file
102Append output from the daemonized process to
103.Pa output_file .
104If the file does not exist, it is created with permissions 0600.
105When this option is used together with options
106.Fl -change-dir
107and
108.Fl -sighup
109the absolute path needs to be provided to ensure
110.Nm
111can re-open the file after a
112.Dv SIGHUP .
113.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile
114Write the ID of the
115.Nm
116process into the
117.Ar supervisor_pidfile
118using the
119.Xr pidfile 3
120functionality.
121The program is executed in a spawned child process while the
122.Nm
123waits until it terminates to keep the
124.Ar supervisor_pidfile
125locked and removes it after the process exits.
126The
127.Ar supervisor_pidfile
128owner is the user who runs the
129.Nm
130regardless of whether the
131.Fl -user
132option is used or not.
133.It Fl p , Fl -child-pidfile Ar child_pidfile
134Write the ID of the created process into the
135.Ar child_pidfile
136using the
137.Xr pidfile 3
138functionality.
139The program is executed in a spawned child process while the
140.Nm
141waits until it terminates to keep the
142.Ar child_pidfile
143locked and removes it after the process exits.
144The
145.Ar child_pidfile
146owner is the user who runs the
147.Nm
148regardless of whether the
149.Fl -user
150option is used or not.
151.It Fl R , Fl -restart-delay Ar restart_delay_seconds
152Supervise and restart the program after the specified delay
153if it has been terminated.
154Valid values are 1-31536000 (up to 1 year).
155.It Fl r , Fl -restart
156Supervise and restart the program after a one-second delay if it has
157been terminated.
158.It Fl S , Fl -syslog
159Enable syslog output.
160This is implicitly applied if other syslog parameters are provided.
161The default values are daemon, notice, and daemon for facility, priority, and
162tag, respectively.
163.It Fl s , Fl -syslog-priority Ar syslog_priority
164These priorities are accepted:
165.Cm emerg , alert , crit , err , warning ,
166.Cm notice , info ,
167and
168.Cm debug .
169The default is
170.Cm notice .
171.It Fl T , Fl -syslog-tag Ar syslog_tag
172Set the tag which is appended to all syslog messages.
173The default is
174.Cm daemon .
175.It Fl t , Fl -title Ar title
176Set the title for the daemon process.
177The default is the daemonized invocation.
178.It Fl u , Fl -user Ar user
179Login name of the user to execute the program under.
180Environment variables
181.Ev HOME , USER ,
182and
183.Ev SHELL
184are set accordingly.
185Requires adequate superuser privileges.
186.El
187.Pp
188If any of the options
189.Fl -child-pidfile ,
190.Fl -output-mask ,
191.Fl -restart ,
192.Fl -restart-delay ,
193.Fl -supervisor-pidfile ,
194.Fl -syslog ,
195.Fl -syslog-facility
196.Fl -syslog-priority ,
197.Fl -syslog-tag ,
198or
199.Fl -output ,
200are specified, the program is executed in a spawned child process.
201The
202.Nm
203waits until it terminates to keep the pid file(s) locked and removes them
204after the process exits or restarts the program.
205In this case if the monitoring
206.Nm
207receives software termination signal
208.Pq Dv SIGTERM
209it forwards it to the
210spawned process.
211Normally it will cause the child to exit, remove the pidfile(s)
212and then terminate.
213.Pp
214If neither file or syslog output are selected, all output is redirected to the
215.Nm
216process and written to stdout.
217The
218.Fl -close-fds
219option may be used to suppress the stdout output completely.
220.Pp
221The
222.Fl -supervisor-pidfile
223option is useful combined with the
224.Fl -restart
225option as
226.Ar supervisor_pidfile
227contains the ID of the supervisor
228not the child.
229This is especially important if you use
230.Fl -restart
231in an rc script as the
232.Fl -child-pidfile
233option will give you the child's ID to signal when you attempt to
234stop the service, causing
235.Nm
236to restart the child.
237.Sh EXIT STATUS
238The
239.Nm
240utility exits 1 if an error is returned by the
241.Xr daemon 3
242library routine, 2 if
243.Ar child_pidfile
244or
245.Ar supervisor_pidfile
246is requested, but cannot be opened, 3 if process is already running (pidfile
247exists and is locked), 4 if
248.Ar syslog_priority
249is not accepted, 5 if
250.Ar syslog_facility
251is not accepted, 6 if
252.Ar output_mask
253is not within the accepted range, 7 if
254.Ar output_file
255cannot be opened for appending, and otherwise 0.
256.Sh DIAGNOSTICS
257If the command cannot be executed, an error message is printed to
258standard error.
259The exact behavior depends on the logging parameters and the
260.Fl -close-fds
261flag.
262.Sh SEE ALSO
263.Xr nohup 1 ,
264.Xr setregid 2 ,
265.Xr setreuid 2 ,
266.Xr daemon 3 ,
267.Xr exec 3 ,
268.Xr pidfile 3 ,
269.Xr termios 4 ,
270.Xr tty 4
271.Sh HISTORY
272The
273.Nm
274utility first appeared in
275.Fx 4.7 .
276