Хотя вы должны узнать о fork()
exec()
wait()
и kill()
, иногда удобнее просто использовать daemon(3)
, если он существует.
Предостережения:
- Нет в POSIX.1-2001
- Не присутствует во всех BSD (однако может называться как-то иначе)
Если переносимость не является серьезной проблемой, это довольно удобно. Если переносимость является серьезной проблемой, вы всегда можете написать свою собственную реализацию и использовать ее.
С справочной страницы:
SYNOPSIS
#include <unistd.h>
int daemon(int nochdir, int noclose);
DESCRIPTION
The daemon() function is for programs wishing to detach themselves from the
controlling terminal and run in the background as system daemons.
If nochdir is zero, daemon() changes the calling process's current working directory
to the root directory ("/"); otherwise, the current working directory is left
unchanged.
If noclose is zero, daemon() redirects standard input, standard output and standard
error to /dev/null; otherwise, no changes are made to these file descriptors.