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