xref: /freebsd/usr.sbin/daemon/daemon.8 (revision 4cd407ec933b81d9d0f733532d29426416cccc06)
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.\"
27bd06a3ecSMike Barcroft.\" $FreeBSD$
28bd06a3ecSMike Barcroft.\"
29*4cd407ecSMaxim Sobolev.Dd September 22, 2020
30bd06a3ecSMike Barcroft.Dt DAEMON 8
31217758f8SRuslan Ermilov.Os
32bd06a3ecSMike Barcroft.Sh NAME
33bd06a3ecSMike Barcroft.Nm daemon
34bd06a3ecSMike Barcroft.Nd run detached from the controlling terminal
35bd06a3ecSMike Barcroft.Sh SYNOPSIS
36217758f8SRuslan Ermilov.Nm
37*4cd407ecSMaxim Sobolev.Op Fl cfHrS
3832b17786SJohn-Mark Gurney.Op Fl p Ar child_pidfile
3932b17786SJohn-Mark Gurney.Op Fl P Ar supervisor_pidfile
40112bfcf5SConrad Meyer.Op Fl t Ar title
41e6d4b388STom Rhodes.Op Fl u Ar user
4253d49b37SJilles Tjoelker.Op Fl m Ar output_mask
4353d49b37SJilles Tjoelker.Op Fl o Ar output_file
4453d49b37SJilles Tjoelker.Op Fl s Ar syslog_priority
4553d49b37SJilles Tjoelker.Op Fl T Ar syslog_tag
4653c2c791SMaxim Konovalov.Op Fl l Ar syslog_facility
47c1ee3587SIan Lepore.Op Fl R Ar restart_delay_seconds
48217758f8SRuslan Ermilov.Ar command arguments ...
49bd06a3ecSMike Barcroft.Sh DESCRIPTION
50bd06a3ecSMike BarcroftThe
51bd06a3ecSMike Barcroft.Nm
52bd06a3ecSMike Barcroftutility detaches itself from the controlling terminal and
53bd06a3ecSMike Barcroftexecutes the program specified by its arguments.
54e6d4b388STom RhodesPrivileges may be lowered to the specified user.
5553d49b37SJilles TjoelkerThe output of the daemonized process may be redirected to syslog and to a
5653d49b37SJilles Tjoelkerlog file.
57bd06a3ecSMike Barcroft.Pp
58bd06a3ecSMike BarcroftThe options are as follows:
59217758f8SRuslan Ermilov.Bl -tag -width indent
60bd06a3ecSMike Barcroft.It Fl c
61217758f8SRuslan ErmilovChange the current working directory to the root
62217758f8SRuslan Ermilov.Pq Dq Pa / .
63bd06a3ecSMike Barcroft.It Fl f
64bd06a3ecSMike BarcroftRedirect standard input, standard output and standard error to
65bd06a3ecSMike Barcroft.Pa /dev/null .
660ec0f10dSIan LeporeWhen this option is used together with any of the options related to file
670ec0f10dSIan Leporeor syslog output, the standard file descriptors are first redirected to
680ec0f10dSIan Lepore.Pa /dev/null ,
690ec0f10dSIan Leporethen stdout and/or stderr is redirected to a file or to syslog as
700ec0f10dSIan Leporespecified by the other options.
71*4cd407ecSMaxim Sobolev.It Fl H
72*4cd407ecSMaxim SobolevClose
73*4cd407ecSMaxim Sobolev.Pa output_file
74*4cd407ecSMaxim Sobolevand re-open it when signal SIGHUP is received, for interoperability with
75*4cd407ecSMaxim Sobolev.Xr newsyslog 1
76*4cd407ecSMaxim Sobolevand similar log rotation / archival mechanisms.  If
77*4cd407ecSMaxim Sobolev.Fa o
78*4cd407ecSMaxim Sobolevis not specified, this flag is ignored.
7953d49b37SJilles Tjoelker.It Fl S
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.
8453d49b37SJilles Tjoelker.It Fl o 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.
88*4cd407ecSMaxim SobolevWhen this option is used together with options
89*4cd407ecSMaxim Sobolev.Fl c and
90*4cd407ecSMaxim Sobolev.Fl H
91*4cd407ecSMaxim Sobolevthe absolute path needs to be provided to ensure
92*4cd407ecSMaxim Sobolev.Nm
93*4cd407ecSMaxim Sobolevcan re-open the file after a SIGHUP.
9453d49b37SJilles Tjoelker.It Fl m Ar output_mask
9553d49b37SJilles TjoelkerRedirect output from the child process stdout (1), stderr (2), or both (3).
9653d49b37SJilles TjoelkerThis value specifies what is sent to syslog and the log file.
9753d49b37SJilles TjoelkerThe default is 3.
9832b17786SJohn-Mark Gurney.It Fl p Ar child_pidfile
992410103cSRuslan ErmilovWrite the ID of the created process into the
10032b17786SJohn-Mark Gurney.Ar child_pidfile
101e6d4b388STom Rhodesusing the
102c6262cb6SPawel Jakub Dawidek.Xr pidfile 3
103c6262cb6SPawel Jakub Dawidekfunctionality.
1042ad43027SMikolaj GolubThe program is executed in a spawned child process while the
1052ad43027SMikolaj Golub.Nm
1062ad43027SMikolaj Golubwaits until it terminates to keep the
10732b17786SJohn-Mark Gurney.Ar child_pidfile
1082ad43027SMikolaj Golublocked and removes it after the process exits.
1092ad43027SMikolaj GolubThe
11032b17786SJohn-Mark Gurney.Ar child_pidfile
11132b17786SJohn-Mark Gurneyowner is the user who runs the
11232b17786SJohn-Mark Gurney.Nm
11332b17786SJohn-Mark Gurneyregardless of whether the
11432b17786SJohn-Mark Gurney.Fl u
11532b17786SJohn-Mark Gurneyoption is used or not.
11632b17786SJohn-Mark Gurney.It Fl P Ar supervisor_pidfile
11732b17786SJohn-Mark GurneyWrite the ID of the
11832b17786SJohn-Mark Gurney.Nm
11932b17786SJohn-Mark Gurneyprocess into the
12032b17786SJohn-Mark Gurney.Ar supervisor_pidfile
12132b17786SJohn-Mark Gurneyusing the
12232b17786SJohn-Mark Gurney.Xr pidfile 3
12332b17786SJohn-Mark Gurneyfunctionality.
12432b17786SJohn-Mark GurneyThe program is executed in a spawned child process while the
12532b17786SJohn-Mark Gurney.Nm
12632b17786SJohn-Mark Gurneywaits until it terminates to keep the
12732b17786SJohn-Mark Gurney.Ar supervisor_pidfile
12832b17786SJohn-Mark Gurneylocked and removes it after the process exits.
12932b17786SJohn-Mark GurneyThe
13032b17786SJohn-Mark Gurney.Ar supervisor_pidfile
1312ad43027SMikolaj Golubowner is the user who runs the
1322ad43027SMikolaj Golub.Nm
1332ad43027SMikolaj Golubregardless of whether the
134704523b0SGuy Helmer.Fl u
1352ad43027SMikolaj Goluboption is used or not.
136b6193c24SMikolaj Golub.It Fl r
13737820b87SIan LeporeSupervise and restart the program after a one-second delay if it has
13837820b87SIan Leporebeen terminated.
13926e19acaSMateusz Piotrowski.It Fl R Ar restart_delay_seconds
14037820b87SIan LeporeSupervise and restart the program after the specified delay
14137820b87SIan Leporeif it has been terminated.
142112bfcf5SConrad Meyer.It Fl t Ar title
14353d49b37SJilles TjoelkerSet the title for the daemon process.
14453d49b37SJilles TjoelkerThe default is the daemonized invocation.
145b6193c24SMikolaj Golub.It Fl u Ar user
146b6193c24SMikolaj GolubLogin name of the user to execute the program under.
147b6193c24SMikolaj GolubRequires adequate superuser privileges.
14853d49b37SJilles Tjoelker.It Fl s Ar syslog_priority
14953d49b37SJilles TjoelkerThese priorities are accepted: emerg, alert, crit, err, warning,
15053d49b37SJilles Tjoelkernotice, info, and debug.
1514f6714d5SMichael GmelinThe default is notice.
15253d49b37SJilles Tjoelker.It Fl l Ar syslog_facility
15353d49b37SJilles TjoelkerThese facilities are accepted: auth, authpriv, console, cron, daemon,
15453d49b37SJilles Tjoelkerftp, kern, lpr, mail, news, ntp, security, syslog, user, uucp, and
15553d49b37SJilles Tjoelkerlocal0, ..., local7.
15653d49b37SJilles TjoelkerThe default is daemon.
15753d49b37SJilles Tjoelker.It Fl T Ar syslog_tag
15853d49b37SJilles TjoelkerSet the tag which is appended to all syslog messages.
15953d49b37SJilles TjoelkerThe default is daemon.
160b6193c24SMikolaj Golub.El
161b6193c24SMikolaj Golub.Pp
16253d49b37SJilles TjoelkerIf any of the options
16332b17786SJohn-Mark Gurney.Fl p ,
16453d49b37SJilles Tjoelker.Fl P ,
16553d49b37SJilles Tjoelker.Fl r ,
16653d49b37SJilles Tjoelker.Fl o ,
16753d49b37SJilles Tjoelker.Fl s ,
16853d49b37SJilles Tjoelker.Fl T ,
16953d49b37SJilles Tjoelker.Fl m ,
17053d49b37SJilles Tjoelker.Fl S ,
171b6193c24SMikolaj Golubor
17253d49b37SJilles Tjoelker.Fl l
17353d49b37SJilles Tjoelkerare specified, the program is executed in a spawned child process.
174b6193c24SMikolaj GolubThe
175b6193c24SMikolaj Golub.Nm
17632b17786SJohn-Mark Gurneywaits until it terminates to keep the pid file(s) locked and removes them
177b6193c24SMikolaj Golubafter the process exits or restarts the program.
178b6193c24SMikolaj GolubIn this case if the monitoring
179195fc497SMikolaj Golub.Nm
180195fc497SMikolaj Golubreceives software termination signal (SIGTERM) it forwards it to the
181195fc497SMikolaj Golubspawned process.
18232b17786SJohn-Mark GurneyNormally it will cause the child to exit, remove the pidfile(s)
18332b17786SJohn-Mark Gurneyand then terminate.
18432b17786SJohn-Mark Gurney.Pp
18553d49b37SJilles TjoelkerIf neither file or syslog output are selected, all output is redirected to the
18653d49b37SJilles Tjoelker.Nm
18753d49b37SJilles Tjoelkerprocess and written to stdout.
18853d49b37SJilles TjoelkerThe
18953d49b37SJilles Tjoelker.Fl f
19053d49b37SJilles Tjoelkeroption may be used to suppress the stdout output completely.
19153d49b37SJilles Tjoelker.Pp
19232b17786SJohn-Mark GurneyThe
19332b17786SJohn-Mark Gurney.Fl P
19432b17786SJohn-Mark Gurneyoption is useful combined with the
19532b17786SJohn-Mark Gurney.Fl r
19632b17786SJohn-Mark Gurneyoption as
19732b17786SJohn-Mark Gurney.Ar supervisor_pidfile
19832b17786SJohn-Mark Gurneycontains the ID of the supervisor
199112bfcf5SConrad Meyernot the child.
200112bfcf5SConrad MeyerThis is especially important if you use
20132b17786SJohn-Mark Gurney.Fl r
20232b17786SJohn-Mark Gurneyin an rc script as the
20332b17786SJohn-Mark Gurney.Fl p
20432b17786SJohn-Mark Gurneyoption will give you the child's ID to signal when you attempt to
20532b17786SJohn-Mark Gurneystop the service, causing
20632b17786SJohn-Mark Gurney.Nm
20732b17786SJohn-Mark Gurneyto restart the child.
208a866e170SRuslan Ermilov.Sh EXIT STATUS
209bd06a3ecSMike BarcroftThe
210bd06a3ecSMike Barcroft.Nm
211bd06a3ecSMike Barcroftutility exits 1 if an error is returned by the
212bd06a3ecSMike Barcroft.Xr daemon 3
21332b17786SJohn-Mark Gurneylibrary routine, 2 if
21432b17786SJohn-Mark Gurney.Ar child_pidfile
21532b17786SJohn-Mark Gurneyor
21632b17786SJohn-Mark Gurney.Ar supervisor_pidfile
217c6262cb6SPawel Jakub Dawidekis requested, but cannot be opened, 3 if process is already running (pidfile
21853d49b37SJilles Tjoelkerexists and is locked), 4 if
21953d49b37SJilles Tjoelker.Ar syslog_priority
22053d49b37SJilles Tjoelkeris not accepted, 5 if
22153d49b37SJilles Tjoelker.Ar syslog_facility
22253d49b37SJilles Tjoelkeris not accepted, 6 if
22353d49b37SJilles Tjoelker.Ar output_mask
22453d49b37SJilles Tjoelkeris not within the accepted range, 7 if
22553d49b37SJilles Tjoelker.Ar output_file
22653d49b37SJilles Tjoelkercannot be opened for appending, and otherwise 0.
227a866e170SRuslan Ermilov.Sh DIAGNOSTICS
22853d49b37SJilles TjoelkerIf the command cannot be executed, an error message is printed to
22953d49b37SJilles Tjoelkerstandard error.
23053d49b37SJilles TjoelkerThe exact behavior depends on the logging parameters and the
231bd06a3ecSMike Barcroft.Fl f
23253d49b37SJilles Tjoelkerflag.
233bd06a3ecSMike Barcroft.Sh SEE ALSO
234a641e445SMateusz Piotrowski.Xr nohup 1 ,
23512d7249eSTom Rhodes.Xr setregid 2 ,
23612d7249eSTom Rhodes.Xr setreuid 2 ,
237bd06a3ecSMike Barcroft.Xr daemon 3 ,
238bd06a3ecSMike Barcroft.Xr exec 3 ,
239c6262cb6SPawel Jakub Dawidek.Xr pidfile 3 ,
240bd06a3ecSMike Barcroft.Xr termios 4 ,
241bd06a3ecSMike Barcroft.Xr tty 4
242bd06a3ecSMike Barcroft.Sh HISTORY
243bd06a3ecSMike BarcroftThe
244bd06a3ecSMike Barcroft.Nm
245bd06a3ecSMike Barcroftutility first appeared in
246a7d4e017STom Rhodes.Fx 4.7 .
247