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