xref: /freebsd/usr.sbin/daemon/daemon.8 (revision 7618c9e1637caea97723ff8accd60f3dc436b044)
1bd06a3ecSMike Barcroft.\" Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
2bd06a3ecSMike Barcroft.\"
3bd06a3ecSMike Barcroft.\" Redistribution and use in source and binary forms, with or without
4bd06a3ecSMike Barcroft.\" modification, are permitted provided that the following conditions
5bd06a3ecSMike Barcroft.\" are met:
6bd06a3ecSMike Barcroft.\" 1. Redistributions of source code must retain the above copyright
7bd06a3ecSMike Barcroft.\"    notice, this list of conditions and the following disclaimer.
8bd06a3ecSMike Barcroft.\" 2. Redistributions in binary form must reproduce the above copyright
9bd06a3ecSMike Barcroft.\"    notice, this list of conditions and the following disclaimer in the
10bd06a3ecSMike Barcroft.\"    documentation and/or other materials provided with the distribution.
11bd06a3ecSMike Barcroft.\" 3. Berkeley Software Design Inc's name may not be used to endorse or
12bd06a3ecSMike Barcroft.\"    promote products derived from this software without specific prior
13bd06a3ecSMike Barcroft.\"    written permission.
14bd06a3ecSMike Barcroft.\"
15bd06a3ecSMike Barcroft.\" THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
16bd06a3ecSMike Barcroft.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17bd06a3ecSMike Barcroft.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18bd06a3ecSMike Barcroft.\" ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
19bd06a3ecSMike Barcroft.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20bd06a3ecSMike Barcroft.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21bd06a3ecSMike Barcroft.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22bd06a3ecSMike Barcroft.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23bd06a3ecSMike Barcroft.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24bd06a3ecSMike Barcroft.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25bd06a3ecSMike Barcroft.\" SUCH DAMAGE.
26bd06a3ecSMike Barcroft.\"
27*7618c9e1SJuraj Lutter.Dd April 25, 2024
28bd06a3ecSMike Barcroft.Dt DAEMON 8
29217758f8SRuslan Ermilov.Os
30bd06a3ecSMike Barcroft.Sh NAME
31bd06a3ecSMike Barcroft.Nm daemon
32bd06a3ecSMike Barcroft.Nd run detached from the controlling terminal
33bd06a3ecSMike Barcroft.Sh SYNOPSIS
34217758f8SRuslan Ermilov.Nm
354cd407ecSMaxim Sobolev.Op Fl cfHrS
3632b17786SJohn-Mark Gurney.Op Fl p Ar child_pidfile
3732b17786SJohn-Mark Gurney.Op Fl P Ar supervisor_pidfile
38112bfcf5SConrad Meyer.Op Fl t Ar title
39e6d4b388STom Rhodes.Op Fl u Ar user
4053d49b37SJilles Tjoelker.Op Fl m Ar output_mask
4153d49b37SJilles Tjoelker.Op Fl o Ar output_file
4253d49b37SJilles Tjoelker.Op Fl s Ar syslog_priority
4353d49b37SJilles Tjoelker.Op Fl T Ar syslog_tag
4453c2c791SMaxim Konovalov.Op Fl l Ar syslog_facility
45c1ee3587SIan Lepore.Op Fl R Ar restart_delay_seconds
46*7618c9e1SJuraj Lutter.Op Fl C Ar restart_count
47217758f8SRuslan Ermilov.Ar command arguments ...
48bd06a3ecSMike Barcroft.Sh DESCRIPTION
49bd06a3ecSMike BarcroftThe
50bd06a3ecSMike Barcroft.Nm
51bd06a3ecSMike Barcroftutility detaches itself from the controlling terminal and
52bd06a3ecSMike Barcroftexecutes the program specified by its arguments.
53e6d4b388STom RhodesPrivileges may be lowered to the specified user.
5453d49b37SJilles TjoelkerThe output of the daemonized process may be redirected to syslog and to a
5553d49b37SJilles Tjoelkerlog file.
56bd06a3ecSMike Barcroft.Pp
57bd06a3ecSMike BarcroftThe options are as follows:
58217758f8SRuslan Ermilov.Bl -tag -width indent
59*7618c9e1SJuraj Lutter.It Fl C , Fl -restart-count Ar restart_count
60*7618c9e1SJuraj LutterRestart the process at most
61*7618c9e1SJuraj Lutter.Ar restart_count
62*7618c9e1SJuraj Luttertimes.
63*7618c9e1SJuraj LutterWhen zero is specified,
64*7618c9e1SJuraj Lutter.Nm
65*7618c9e1SJuraj Lutterwill exit.
66*7618c9e1SJuraj LutterThe maximum restart count is
67*7618c9e1SJuraj Lutter.Cm 128 .
68*7618c9e1SJuraj LutterThis option is used together with option
69*7618c9e1SJuraj Lutter.Fl -restart .
700a402ad2SIhor Antonov.It Fl c , Fl -change-dir
71217758f8SRuslan ErmilovChange the current working directory to the root
72217758f8SRuslan Ermilov.Pq Dq Pa / .
730a402ad2SIhor Antonov.It Fl f , Fl -close-fds
74bd06a3ecSMike BarcroftRedirect standard input, standard output and standard error to
75bd06a3ecSMike Barcroft.Pa /dev/null .
760ec0f10dSIan LeporeWhen this option is used together with any of the options related to file
770ec0f10dSIan Leporeor syslog output, the standard file descriptors are first redirected to
780ec0f10dSIan Lepore.Pa /dev/null ,
790ec0f10dSIan Leporethen stdout and/or stderr is redirected to a file or to syslog as
800ec0f10dSIan Leporespecified by the other options.
810a402ad2SIhor Antonov.It Fl H , Fl -sighup
824cd407ecSMaxim SobolevClose
834cd407ecSMaxim Sobolev.Pa output_file
84735637f7SMateusz Piotrowskiand re-open it when signal
85735637f7SMateusz Piotrowski.Dv SIGHUP
86735637f7SMateusz Piotrowskiis received, for interoperability with
87*7618c9e1SJuraj Lutter.Xr newsyslog 8
88*7618c9e1SJuraj Lutterand similar log rotation / archival mechanisms.
89*7618c9e1SJuraj LutterIf
900a402ad2SIhor Antonov.Fl -output-file
914cd407ecSMaxim Sobolevis not specified, this flag is ignored.
92ee23e1e4SMateusz Piotrowski.It Fl l , Fl -syslog-facility Ar syslog_facility
93ee23e1e4SMateusz PiotrowskiThese facilities are accepted:
94ee23e1e4SMateusz Piotrowski.Cm auth , authpriv , console , cron , daemon ,
95ee23e1e4SMateusz Piotrowski.Cm ftp , kern , lpr , mail , news , ntp , security ,
96ee23e1e4SMateusz Piotrowski.Cm syslog , user , uucp ,
97ee23e1e4SMateusz Piotrowskiand
98ee23e1e4SMateusz Piotrowskilocal facilities from
99ee23e1e4SMateusz Piotrowski.Cm local0
100ee23e1e4SMateusz Piotrowskito
101ee23e1e4SMateusz Piotrowski.Cm local7 .
102ee23e1e4SMateusz PiotrowskiThe default is
103ee23e1e4SMateusz Piotrowski.Cm daemon .
104ee23e1e4SMateusz Piotrowski.It Fl m , Fl -output-mask Ar output_mask
105ee23e1e4SMateusz PiotrowskiRedirect output from the child process stdout
106ee23e1e4SMateusz Piotrowski.Pq Cm 1 ,
107ee23e1e4SMateusz Piotrowskistderr
108ee23e1e4SMateusz Piotrowski.Pq Cm 2 ,
109ee23e1e4SMateusz Piotrowskior both
110ee23e1e4SMateusz Piotrowski.Pq Cm 3 .
111ee23e1e4SMateusz PiotrowskiThis value specifies what is sent to syslog and the log file.
112ee23e1e4SMateusz PiotrowskiThe default is
113ee23e1e4SMateusz Piotrowski.Cm 3 .
1140a402ad2SIhor Antonov.It Fl o , Fl -output-file Ar output_file
11553d49b37SJilles TjoelkerAppend output from the daemonized process to
11653d49b37SJilles Tjoelker.Pa output_file .
11753d49b37SJilles TjoelkerIf the file does not exist, it is created with permissions 0600.
1184cd407ecSMaxim SobolevWhen this option is used together with options
1190a402ad2SIhor Antonov.Fl -change-dir
12054c743e5SMateusz Piotrowskiand
1210a402ad2SIhor Antonov.Fl -sighup
1224cd407ecSMaxim Sobolevthe absolute path needs to be provided to ensure
1234cd407ecSMaxim Sobolev.Nm
124735637f7SMateusz Piotrowskican re-open the file after a
125735637f7SMateusz Piotrowski.Dv SIGHUP .
1260a402ad2SIhor Antonov.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile
12732b17786SJohn-Mark GurneyWrite the ID of the
12832b17786SJohn-Mark Gurney.Nm
12932b17786SJohn-Mark Gurneyprocess into the
13032b17786SJohn-Mark Gurney.Ar supervisor_pidfile
13132b17786SJohn-Mark Gurneyusing the
13232b17786SJohn-Mark Gurney.Xr pidfile 3
13332b17786SJohn-Mark Gurneyfunctionality.
13432b17786SJohn-Mark GurneyThe program is executed in a spawned child process while the
13532b17786SJohn-Mark Gurney.Nm
13632b17786SJohn-Mark Gurneywaits until it terminates to keep the
13732b17786SJohn-Mark Gurney.Ar supervisor_pidfile
13832b17786SJohn-Mark Gurneylocked and removes it after the process exits.
13932b17786SJohn-Mark GurneyThe
14032b17786SJohn-Mark Gurney.Ar supervisor_pidfile
1412ad43027SMikolaj Golubowner is the user who runs the
1422ad43027SMikolaj Golub.Nm
1432ad43027SMikolaj Golubregardless of whether the
1440a402ad2SIhor Antonov.Fl -user
1452ad43027SMikolaj Goluboption is used or not.
146ee23e1e4SMateusz Piotrowski.It Fl p , Fl -child-pidfile Ar child_pidfile
147ee23e1e4SMateusz PiotrowskiWrite the ID of the created process into the
148ee23e1e4SMateusz Piotrowski.Ar child_pidfile
149ee23e1e4SMateusz Piotrowskiusing the
150ee23e1e4SMateusz Piotrowski.Xr pidfile 3
151ee23e1e4SMateusz Piotrowskifunctionality.
152ee23e1e4SMateusz PiotrowskiThe program is executed in a spawned child process while the
153ee23e1e4SMateusz Piotrowski.Nm
154ee23e1e4SMateusz Piotrowskiwaits until it terminates to keep the
155ee23e1e4SMateusz Piotrowski.Ar child_pidfile
156ee23e1e4SMateusz Piotrowskilocked and removes it after the process exits.
157ee23e1e4SMateusz PiotrowskiThe
158ee23e1e4SMateusz Piotrowski.Ar child_pidfile
159ee23e1e4SMateusz Piotrowskiowner is the user who runs the
160ee23e1e4SMateusz Piotrowski.Nm
161ee23e1e4SMateusz Piotrowskiregardless of whether the
162ee23e1e4SMateusz Piotrowski.Fl -user
163ee23e1e4SMateusz Piotrowskioption is used or not.
1640a402ad2SIhor Antonov.It Fl R , Fl -restart-delay Ar restart_delay_seconds
16537820b87SIan LeporeSupervise and restart the program after the specified delay
16637820b87SIan Leporeif it has been terminated.
167a6f795ccSIhor AntonovValid values are 1-31536000 (up to 1 year).
168ee23e1e4SMateusz Piotrowski.It Fl r , Fl -restart
169ee23e1e4SMateusz PiotrowskiSupervise and restart the program after a one-second delay if it has
170ee23e1e4SMateusz Piotrowskibeen terminated.
171ee23e1e4SMateusz Piotrowski.It Fl S , Fl -syslog
172ee23e1e4SMateusz PiotrowskiEnable syslog output.
173ee23e1e4SMateusz PiotrowskiThis is implicitly applied if other syslog parameters are provided.
174ee23e1e4SMateusz PiotrowskiThe default values are daemon, notice, and daemon for facility, priority, and
175ee23e1e4SMateusz Piotrowskitag, respectively.
1760a402ad2SIhor Antonov.It Fl s , Fl -syslog-priority Ar syslog_priority
177a5bc8e8aSMateusz PiotrowskiThese priorities are accepted:
178a5bc8e8aSMateusz Piotrowski.Cm emerg , alert , crit , err , warning ,
179a5bc8e8aSMateusz Piotrowski.Cm notice , info ,
180a5bc8e8aSMateusz Piotrowskiand
181a5bc8e8aSMateusz Piotrowski.Cm debug .
182a5bc8e8aSMateusz PiotrowskiThe default is
183a5bc8e8aSMateusz Piotrowski.Cm notice .
1840a402ad2SIhor Antonov.It Fl T , Fl -syslog-tag Ar syslog_tag
18553d49b37SJilles TjoelkerSet the tag which is appended to all syslog messages.
186a5bc8e8aSMateusz PiotrowskiThe default is
187a5bc8e8aSMateusz Piotrowski.Cm daemon .
188ee23e1e4SMateusz Piotrowski.It Fl t , Fl -title Ar title
189ee23e1e4SMateusz PiotrowskiSet the title for the daemon process.
190ee23e1e4SMateusz PiotrowskiThe default is the daemonized invocation.
191ee23e1e4SMateusz Piotrowski.It Fl u , Fl -user Ar user
192ee23e1e4SMateusz PiotrowskiLogin name of the user to execute the program under.
193c1207678SMateusz PiotrowskiEnvironment variables
194c1207678SMateusz Piotrowski.Ev HOME , USER ,
195c1207678SMateusz Piotrowskiand
196c1207678SMateusz Piotrowski.Ev SHELL
197c1207678SMateusz Piotrowskiare set accordingly.
198ee23e1e4SMateusz PiotrowskiRequires adequate superuser privileges.
199b6193c24SMikolaj Golub.El
200b6193c24SMikolaj Golub.Pp
20153d49b37SJilles TjoelkerIf any of the options
2020a402ad2SIhor Antonov.Fl -child-pidfile ,
2030a402ad2SIhor Antonov.Fl -output-mask ,
2040a402ad2SIhor Antonov.Fl -restart ,
2050a402ad2SIhor Antonov.Fl -restart-delay ,
2060a402ad2SIhor Antonov.Fl -supervisor-pidfile ,
2070a402ad2SIhor Antonov.Fl -syslog ,
2080a402ad2SIhor Antonov.Fl -syslog-facility
2090a402ad2SIhor Antonov.Fl -syslog-priority ,
2100a402ad2SIhor Antonov.Fl -syslog-tag ,
211b6193c24SMikolaj Golubor
2120a402ad2SIhor Antonov.Fl -output ,
21353d49b37SJilles Tjoelkerare specified, the program is executed in a spawned child process.
214b6193c24SMikolaj GolubThe
215b6193c24SMikolaj Golub.Nm
21632b17786SJohn-Mark Gurneywaits until it terminates to keep the pid file(s) locked and removes them
217b6193c24SMikolaj Golubafter the process exits or restarts the program.
218b6193c24SMikolaj GolubIn this case if the monitoring
219195fc497SMikolaj Golub.Nm
220735637f7SMateusz Piotrowskireceives software termination signal
221735637f7SMateusz Piotrowski.Pq Dv SIGTERM
222735637f7SMateusz Piotrowskiit forwards it to the
223195fc497SMikolaj Golubspawned process.
22432b17786SJohn-Mark GurneyNormally it will cause the child to exit, remove the pidfile(s)
22532b17786SJohn-Mark Gurneyand then terminate.
22632b17786SJohn-Mark Gurney.Pp
22753d49b37SJilles TjoelkerIf neither file or syslog output are selected, all output is redirected to the
22853d49b37SJilles Tjoelker.Nm
22953d49b37SJilles Tjoelkerprocess and written to stdout.
23053d49b37SJilles TjoelkerThe
2310a402ad2SIhor Antonov.Fl -close-fds
23253d49b37SJilles Tjoelkeroption may be used to suppress the stdout output completely.
23353d49b37SJilles Tjoelker.Pp
23432b17786SJohn-Mark GurneyThe
2350a402ad2SIhor Antonov.Fl -supervisor-pidfile
23632b17786SJohn-Mark Gurneyoption is useful combined with the
2370a402ad2SIhor Antonov.Fl -restart
23832b17786SJohn-Mark Gurneyoption as
23932b17786SJohn-Mark Gurney.Ar supervisor_pidfile
24032b17786SJohn-Mark Gurneycontains the ID of the supervisor
241112bfcf5SConrad Meyernot the child.
242112bfcf5SConrad MeyerThis is especially important if you use
2430a402ad2SIhor Antonov.Fl -restart
24432b17786SJohn-Mark Gurneyin an rc script as the
2450a402ad2SIhor Antonov.Fl -child-pidfile
24632b17786SJohn-Mark Gurneyoption will give you the child's ID to signal when you attempt to
24732b17786SJohn-Mark Gurneystop the service, causing
24832b17786SJohn-Mark Gurney.Nm
24932b17786SJohn-Mark Gurneyto restart the child.
250a866e170SRuslan Ermilov.Sh EXIT STATUS
251bd06a3ecSMike BarcroftThe
252bd06a3ecSMike Barcroft.Nm
253bd06a3ecSMike Barcroftutility exits 1 if an error is returned by the
254bd06a3ecSMike Barcroft.Xr daemon 3
25532b17786SJohn-Mark Gurneylibrary routine, 2 if
25632b17786SJohn-Mark Gurney.Ar child_pidfile
25732b17786SJohn-Mark Gurneyor
25832b17786SJohn-Mark Gurney.Ar supervisor_pidfile
259c6262cb6SPawel Jakub Dawidekis requested, but cannot be opened, 3 if process is already running (pidfile
26053d49b37SJilles Tjoelkerexists and is locked), 4 if
26153d49b37SJilles Tjoelker.Ar syslog_priority
26253d49b37SJilles Tjoelkeris not accepted, 5 if
26353d49b37SJilles Tjoelker.Ar syslog_facility
26453d49b37SJilles Tjoelkeris not accepted, 6 if
26553d49b37SJilles Tjoelker.Ar output_mask
26653d49b37SJilles Tjoelkeris not within the accepted range, 7 if
26753d49b37SJilles Tjoelker.Ar output_file
26853d49b37SJilles Tjoelkercannot be opened for appending, and otherwise 0.
269a866e170SRuslan Ermilov.Sh DIAGNOSTICS
27053d49b37SJilles TjoelkerIf the command cannot be executed, an error message is printed to
27153d49b37SJilles Tjoelkerstandard error.
27253d49b37SJilles TjoelkerThe exact behavior depends on the logging parameters and the
2730a402ad2SIhor Antonov.Fl -close-fds
27453d49b37SJilles Tjoelkerflag.
275bd06a3ecSMike Barcroft.Sh SEE ALSO
276a641e445SMateusz Piotrowski.Xr nohup 1 ,
27712d7249eSTom Rhodes.Xr setregid 2 ,
27812d7249eSTom Rhodes.Xr setreuid 2 ,
279bd06a3ecSMike Barcroft.Xr daemon 3 ,
280bd06a3ecSMike Barcroft.Xr exec 3 ,
281c6262cb6SPawel Jakub Dawidek.Xr pidfile 3 ,
282bd06a3ecSMike Barcroft.Xr termios 4 ,
283bd06a3ecSMike Barcroft.Xr tty 4
284bd06a3ecSMike Barcroft.Sh HISTORY
285bd06a3ecSMike BarcroftThe
286bd06a3ecSMike Barcroft.Nm
287bd06a3ecSMike Barcroftutility first appeared in
288a7d4e017STom Rhodes.Fx 4.7 .
289