xref: /freebsd/usr.sbin/daemon/daemon.8 (revision a5bc8e8a5863c4aea5ed7ebe2ebc2332c287545c)
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.\"
2754c743e5SMateusz Piotrowski.Dd January 14, 2021
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
46217758f8SRuslan Ermilov.Ar command arguments ...
47bd06a3ecSMike Barcroft.Sh DESCRIPTION
48bd06a3ecSMike BarcroftThe
49bd06a3ecSMike Barcroft.Nm
50bd06a3ecSMike Barcroftutility detaches itself from the controlling terminal and
51bd06a3ecSMike Barcroftexecutes the program specified by its arguments.
52e6d4b388STom RhodesPrivileges may be lowered to the specified user.
5353d49b37SJilles TjoelkerThe output of the daemonized process may be redirected to syslog and to a
5453d49b37SJilles Tjoelkerlog file.
55bd06a3ecSMike Barcroft.Pp
56bd06a3ecSMike BarcroftThe options are as follows:
57217758f8SRuslan Ermilov.Bl -tag -width indent
580a402ad2SIhor Antonov.It Fl c , Fl -change-dir
59217758f8SRuslan ErmilovChange the current working directory to the root
60217758f8SRuslan Ermilov.Pq Dq Pa / .
610a402ad2SIhor Antonov.It Fl f , Fl -close-fds
62bd06a3ecSMike BarcroftRedirect standard input, standard output and standard error to
63bd06a3ecSMike Barcroft.Pa /dev/null .
640ec0f10dSIan LeporeWhen this option is used together with any of the options related to file
650ec0f10dSIan Leporeor syslog output, the standard file descriptors are first redirected to
660ec0f10dSIan Lepore.Pa /dev/null ,
670ec0f10dSIan Leporethen stdout and/or stderr is redirected to a file or to syslog as
680ec0f10dSIan Leporespecified by the other options.
690a402ad2SIhor Antonov.It Fl H , Fl -sighup
704cd407ecSMaxim SobolevClose
714cd407ecSMaxim Sobolev.Pa output_file
72735637f7SMateusz Piotrowskiand re-open it when signal
73735637f7SMateusz Piotrowski.Dv SIGHUP
74735637f7SMateusz Piotrowskiis received, for interoperability with
754cd407ecSMaxim Sobolev.Xr newsyslog 1
764cd407ecSMaxim Sobolevand similar log rotation / archival mechanisms.  If
770a402ad2SIhor Antonov.Fl -output-file
784cd407ecSMaxim Sobolevis not specified, this flag is ignored.
790a402ad2SIhor Antonov.It Fl S , Fl -syslog
8053d49b37SJilles TjoelkerEnable syslog output.
8153d49b37SJilles TjoelkerThis is implicitly applied if other syslog parameters are provided.
8253d49b37SJilles TjoelkerThe default values are daemon, notice, and daemon for facility, priority, and
8353d49b37SJilles Tjoelkertag, respectively.
840a402ad2SIhor Antonov.It Fl o , Fl -output-file Ar output_file
8553d49b37SJilles TjoelkerAppend output from the daemonized process to
8653d49b37SJilles Tjoelker.Pa output_file .
8753d49b37SJilles TjoelkerIf the file does not exist, it is created with permissions 0600.
884cd407ecSMaxim SobolevWhen this option is used together with options
890a402ad2SIhor Antonov.Fl -change-dir
9054c743e5SMateusz Piotrowskiand
910a402ad2SIhor Antonov.Fl -sighup
924cd407ecSMaxim Sobolevthe absolute path needs to be provided to ensure
934cd407ecSMaxim Sobolev.Nm
94735637f7SMateusz Piotrowskican re-open the file after a
95735637f7SMateusz Piotrowski.Dv SIGHUP .
960a402ad2SIhor Antonov.It Fl m , Fl -output-mask Ar output_mask
97*a5bc8e8aSMateusz PiotrowskiRedirect output from the child process stdout
98*a5bc8e8aSMateusz Piotrowski.Pq Cm 1 ,
99*a5bc8e8aSMateusz Piotrowskistderr
100*a5bc8e8aSMateusz Piotrowski.Pq Cm 2 ,
101*a5bc8e8aSMateusz Piotrowskior both
102*a5bc8e8aSMateusz Piotrowski.Pq Cm 3 .
10353d49b37SJilles TjoelkerThis value specifies what is sent to syslog and the log file.
104*a5bc8e8aSMateusz PiotrowskiThe default is
105*a5bc8e8aSMateusz Piotrowski.Cm 3 .
1060a402ad2SIhor Antonov.It Fl p , Fl -child-pidfile Ar child_pidfile
1072410103cSRuslan ErmilovWrite the ID of the created process into the
10832b17786SJohn-Mark Gurney.Ar child_pidfile
109e6d4b388STom Rhodesusing the
110c6262cb6SPawel Jakub Dawidek.Xr pidfile 3
111c6262cb6SPawel Jakub Dawidekfunctionality.
1122ad43027SMikolaj GolubThe program is executed in a spawned child process while the
1132ad43027SMikolaj Golub.Nm
1142ad43027SMikolaj Golubwaits until it terminates to keep the
11532b17786SJohn-Mark Gurney.Ar child_pidfile
1162ad43027SMikolaj Golublocked and removes it after the process exits.
1172ad43027SMikolaj GolubThe
11832b17786SJohn-Mark Gurney.Ar child_pidfile
11932b17786SJohn-Mark Gurneyowner is the user who runs the
12032b17786SJohn-Mark Gurney.Nm
12132b17786SJohn-Mark Gurneyregardless of whether the
1220a402ad2SIhor Antonov.Fl -user
12332b17786SJohn-Mark Gurneyoption is used or not.
1240a402ad2SIhor Antonov.It Fl P , Fl -supervisor-pidfile Ar supervisor_pidfile
12532b17786SJohn-Mark GurneyWrite the ID of the
12632b17786SJohn-Mark Gurney.Nm
12732b17786SJohn-Mark Gurneyprocess into the
12832b17786SJohn-Mark Gurney.Ar supervisor_pidfile
12932b17786SJohn-Mark Gurneyusing the
13032b17786SJohn-Mark Gurney.Xr pidfile 3
13132b17786SJohn-Mark Gurneyfunctionality.
13232b17786SJohn-Mark GurneyThe program is executed in a spawned child process while the
13332b17786SJohn-Mark Gurney.Nm
13432b17786SJohn-Mark Gurneywaits until it terminates to keep the
13532b17786SJohn-Mark Gurney.Ar supervisor_pidfile
13632b17786SJohn-Mark Gurneylocked and removes it after the process exits.
13732b17786SJohn-Mark GurneyThe
13832b17786SJohn-Mark Gurney.Ar supervisor_pidfile
1392ad43027SMikolaj Golubowner is the user who runs the
1402ad43027SMikolaj Golub.Nm
1412ad43027SMikolaj Golubregardless of whether the
1420a402ad2SIhor Antonov.Fl -user
1432ad43027SMikolaj Goluboption is used or not.
1440a402ad2SIhor Antonov.It Fl r , Fl -restart
14537820b87SIan LeporeSupervise and restart the program after a one-second delay if it has
14637820b87SIan Leporebeen terminated.
1470a402ad2SIhor Antonov.It Fl R , Fl -restart-delay Ar restart_delay_seconds
14837820b87SIan LeporeSupervise and restart the program after the specified delay
14937820b87SIan Leporeif it has been terminated.
1500a402ad2SIhor Antonov.It Fl t , Fl -title Ar title
15153d49b37SJilles TjoelkerSet the title for the daemon process.
15253d49b37SJilles TjoelkerThe default is the daemonized invocation.
1530a402ad2SIhor Antonov.It Fl u , Fl -user Ar user
154b6193c24SMikolaj GolubLogin name of the user to execute the program under.
155b6193c24SMikolaj GolubRequires adequate superuser privileges.
1560a402ad2SIhor Antonov.It Fl s , Fl -syslog-priority Ar syslog_priority
157*a5bc8e8aSMateusz PiotrowskiThese priorities are accepted:
158*a5bc8e8aSMateusz Piotrowski.Cm emerg , alert , crit , err , warning ,
159*a5bc8e8aSMateusz Piotrowski.Cm notice , info ,
160*a5bc8e8aSMateusz Piotrowskiand
161*a5bc8e8aSMateusz Piotrowski.Cm debug .
162*a5bc8e8aSMateusz PiotrowskiThe default is
163*a5bc8e8aSMateusz Piotrowski.Cm notice .
1640a402ad2SIhor Antonov.It Fl l , Fl -syslog-facility Ar syslog_facility
165*a5bc8e8aSMateusz PiotrowskiThese facilities are accepted:
166*a5bc8e8aSMateusz Piotrowski.Cm auth , authpriv , console , cron , daemon ,
167*a5bc8e8aSMateusz Piotrowski.Cm ftp , kern , lpr , mail , news , ntp , security ,
168*a5bc8e8aSMateusz Piotrowski.Cm syslog , user , uucp ,
169*a5bc8e8aSMateusz Piotrowskiand
170*a5bc8e8aSMateusz Piotrowskilocal facilities from
171*a5bc8e8aSMateusz Piotrowski.Cm local0
172*a5bc8e8aSMateusz Piotrowskito
173*a5bc8e8aSMateusz Piotrowski.Cm local7 .
174*a5bc8e8aSMateusz PiotrowskiThe default is
175*a5bc8e8aSMateusz Piotrowski.Cm daemon .
1760a402ad2SIhor Antonov.It Fl T , Fl -syslog-tag Ar syslog_tag
17753d49b37SJilles TjoelkerSet the tag which is appended to all syslog messages.
178*a5bc8e8aSMateusz PiotrowskiThe default is
179*a5bc8e8aSMateusz Piotrowski.Cm daemon .
180b6193c24SMikolaj Golub.El
181b6193c24SMikolaj Golub.Pp
18253d49b37SJilles TjoelkerIf any of the options
1830a402ad2SIhor Antonov.Fl -child-pidfile ,
1840a402ad2SIhor Antonov.Fl -output-mask ,
1850a402ad2SIhor Antonov.Fl -restart ,
1860a402ad2SIhor Antonov.Fl -restart-delay ,
1870a402ad2SIhor Antonov.Fl -supervisor-pidfile ,
1880a402ad2SIhor Antonov.Fl -syslog ,
1890a402ad2SIhor Antonov.Fl -syslog-facility
1900a402ad2SIhor Antonov.Fl -syslog-priority ,
1910a402ad2SIhor Antonov.Fl -syslog-tag ,
192b6193c24SMikolaj Golubor
1930a402ad2SIhor Antonov.Fl -output ,
19453d49b37SJilles Tjoelkerare specified, the program is executed in a spawned child process.
195b6193c24SMikolaj GolubThe
196b6193c24SMikolaj Golub.Nm
19732b17786SJohn-Mark Gurneywaits until it terminates to keep the pid file(s) locked and removes them
198b6193c24SMikolaj Golubafter the process exits or restarts the program.
199b6193c24SMikolaj GolubIn this case if the monitoring
200195fc497SMikolaj Golub.Nm
201735637f7SMateusz Piotrowskireceives software termination signal
202735637f7SMateusz Piotrowski.Pq Dv SIGTERM
203735637f7SMateusz Piotrowskiit forwards it to the
204195fc497SMikolaj Golubspawned process.
20532b17786SJohn-Mark GurneyNormally it will cause the child to exit, remove the pidfile(s)
20632b17786SJohn-Mark Gurneyand then terminate.
20732b17786SJohn-Mark Gurney.Pp
20853d49b37SJilles TjoelkerIf neither file or syslog output are selected, all output is redirected to the
20953d49b37SJilles Tjoelker.Nm
21053d49b37SJilles Tjoelkerprocess and written to stdout.
21153d49b37SJilles TjoelkerThe
2120a402ad2SIhor Antonov.Fl -close-fds
21353d49b37SJilles Tjoelkeroption may be used to suppress the stdout output completely.
21453d49b37SJilles Tjoelker.Pp
21532b17786SJohn-Mark GurneyThe
2160a402ad2SIhor Antonov.Fl -supervisor-pidfile
21732b17786SJohn-Mark Gurneyoption is useful combined with the
2180a402ad2SIhor Antonov.Fl -restart
21932b17786SJohn-Mark Gurneyoption as
22032b17786SJohn-Mark Gurney.Ar supervisor_pidfile
22132b17786SJohn-Mark Gurneycontains the ID of the supervisor
222112bfcf5SConrad Meyernot the child.
223112bfcf5SConrad MeyerThis is especially important if you use
2240a402ad2SIhor Antonov.Fl -restart
22532b17786SJohn-Mark Gurneyin an rc script as the
2260a402ad2SIhor Antonov.Fl -child-pidfile
22732b17786SJohn-Mark Gurneyoption will give you the child's ID to signal when you attempt to
22832b17786SJohn-Mark Gurneystop the service, causing
22932b17786SJohn-Mark Gurney.Nm
23032b17786SJohn-Mark Gurneyto restart the child.
231a866e170SRuslan Ermilov.Sh EXIT STATUS
232bd06a3ecSMike BarcroftThe
233bd06a3ecSMike Barcroft.Nm
234bd06a3ecSMike Barcroftutility exits 1 if an error is returned by the
235bd06a3ecSMike Barcroft.Xr daemon 3
23632b17786SJohn-Mark Gurneylibrary routine, 2 if
23732b17786SJohn-Mark Gurney.Ar child_pidfile
23832b17786SJohn-Mark Gurneyor
23932b17786SJohn-Mark Gurney.Ar supervisor_pidfile
240c6262cb6SPawel Jakub Dawidekis requested, but cannot be opened, 3 if process is already running (pidfile
24153d49b37SJilles Tjoelkerexists and is locked), 4 if
24253d49b37SJilles Tjoelker.Ar syslog_priority
24353d49b37SJilles Tjoelkeris not accepted, 5 if
24453d49b37SJilles Tjoelker.Ar syslog_facility
24553d49b37SJilles Tjoelkeris not accepted, 6 if
24653d49b37SJilles Tjoelker.Ar output_mask
24753d49b37SJilles Tjoelkeris not within the accepted range, 7 if
24853d49b37SJilles Tjoelker.Ar output_file
24953d49b37SJilles Tjoelkercannot be opened for appending, and otherwise 0.
250a866e170SRuslan Ermilov.Sh DIAGNOSTICS
25153d49b37SJilles TjoelkerIf the command cannot be executed, an error message is printed to
25253d49b37SJilles Tjoelkerstandard error.
25353d49b37SJilles TjoelkerThe exact behavior depends on the logging parameters and the
2540a402ad2SIhor Antonov.Fl -close-fds
25553d49b37SJilles Tjoelkerflag.
256bd06a3ecSMike Barcroft.Sh SEE ALSO
257a641e445SMateusz Piotrowski.Xr nohup 1 ,
25812d7249eSTom Rhodes.Xr setregid 2 ,
25912d7249eSTom Rhodes.Xr setreuid 2 ,
260bd06a3ecSMike Barcroft.Xr daemon 3 ,
261bd06a3ecSMike Barcroft.Xr exec 3 ,
262c6262cb6SPawel Jakub Dawidek.Xr pidfile 3 ,
263bd06a3ecSMike Barcroft.Xr termios 4 ,
264bd06a3ecSMike Barcroft.Xr tty 4
265bd06a3ecSMike Barcroft.Sh HISTORY
266bd06a3ecSMike BarcroftThe
267bd06a3ecSMike Barcroft.Nm
268bd06a3ecSMike Barcroftutility first appeared in
269a7d4e017STom Rhodes.Fx 4.7 .
270