xref: /freebsd/usr.sbin/daemon/daemon.8 (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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.\" $FreeBSD$
28.\"
29.Dd March 2, 2016
30.Dt DAEMON 8
31.Os
32.Sh NAME
33.Nm daemon
34.Nd run detached from the controlling terminal
35.Sh SYNOPSIS
36.Nm
37.Op Fl cfr
38.Op Fl p Ar child_pidfile
39.Op Fl P Ar supervisor_pidfile
40.Op Fl t Ar title
41.Op Fl u Ar user
42.Ar command arguments ...
43.Sh DESCRIPTION
44The
45.Nm
46utility detaches itself from the controlling terminal and
47executes the program specified by its arguments.
48Privileges may be lowered to the specified user.
49.Pp
50The options are as follows:
51.Bl -tag -width indent
52.It Fl c
53Change the current working directory to the root
54.Pq Dq Pa / .
55.It Fl f
56Redirect standard input, standard output and standard error to
57.Pa /dev/null .
58.It Fl p Ar child_pidfile
59Write the ID of the created process into the
60.Ar child_pidfile
61using the
62.Xr pidfile 3
63functionality.
64The program is executed in a spawned child process while the
65.Nm
66waits until it terminates to keep the
67.Ar child_pidfile
68locked and removes it after the process exits.
69The
70.Ar child_pidfile
71owner is the user who runs the
72.Nm
73regardless of whether the
74.Fl u
75option is used or not.
76.It Fl P Ar supervisor_pidfile
77Write the ID of the
78.Nm
79process into the
80.Ar supervisor_pidfile
81using the
82.Xr pidfile 3
83functionality.
84The program is executed in a spawned child process while the
85.Nm
86waits until it terminates to keep the
87.Ar supervisor_pidfile
88locked and removes it after the process exits.
89The
90.Ar supervisor_pidfile
91owner is the user who runs the
92.Nm
93regardless of whether the
94.Fl u
95option is used or not.
96.It Fl r
97Supervise and restart the program if it has been terminated.
98.It Fl t Ar title
99Process title for the daemon to make it easily identifiable.
100.It Fl u Ar user
101Login name of the user to execute the program under.
102Requires adequate superuser privileges.
103.El
104.Pp
105If the
106.Fl p ,
107.Fl P
108or
109.Fl r
110option is specified the program is executed in a spawned child process.
111The
112.Nm
113waits until it terminates to keep the pid file(s) locked and removes them
114after the process exits or restarts the program.
115In this case if the monitoring
116.Nm
117receives software termination signal (SIGTERM) it forwards it to the
118spawned process.
119Normally it will cause the child to exit, remove the pidfile(s)
120and then terminate.
121.Pp
122The
123.Fl P
124option is useful combined with the
125.Fl r
126option as
127.Ar supervisor_pidfile
128contains the ID of the supervisor
129not the child.
130This is especially important if you use
131.Fl r
132in an rc script as the
133.Fl p
134option will give you the child's ID to signal when you attempt to
135stop the service, causing
136.Nm
137to restart the child.
138.Sh EXIT STATUS
139The
140.Nm
141utility exits 1 if an error is returned by the
142.Xr daemon 3
143library routine, 2 if
144.Ar child_pidfile
145or
146.Ar supervisor_pidfile
147is requested, but cannot be opened, 3 if process is already running (pidfile
148exists and is locked),
149otherwise 0.
150.Sh DIAGNOSTICS
151If the command cannot be executed, an error message is displayed on
152standard error unless the
153.Fl f
154flag is specified.
155.Sh SEE ALSO
156.Xr setregid 2 ,
157.Xr setreuid 2 ,
158.Xr daemon 3 ,
159.Xr exec 3 ,
160.Xr pidfile 3 ,
161.Xr termios 4 ,
162.Xr tty 4
163.Sh HISTORY
164The
165.Nm
166utility first appeared in
167.Fx 4.7 .
168