daemon - run in the background
daemon(int nochdir, int noclose);
The daemon() function is for programs wishing to detach
the controlling terminal and run in the background as system
Unless the argument nochdir is non-zero, daemon() changes
working directory to the root (/).
Unless the argument noclose is non-zero, daemon() will redirect standard
input, standard output and standard error to /dev/null.
Upon success, daemon() returns 0; otherwise -1 is returned.
The function daemon() may fail and set errno for any of the
for the library functions fork(2) and setsid(2).
The daemon() function first appeared in 4.4BSD.
Unless the noclose argument is non-zero, daemon() will close
three file descriptors and redirect them to /dev/null. Normally, these
correspond to standard input, standard output and standard
if any of those file descriptors refer to something else
still be closed, resulting in incorrect behavior of the
This can happen if any of standard input, standard output or
have been closed before the program was run. Programs
should therefore make sure to either call daemon() before
files or sockets or, alternately, verifying that any file
have a value greater than 2.
OpenBSD 3.6 June 9, 1993
[ Back ]