Shell Process Daemon – Difference Between Running a Program as a Daemon and Forking It into Background with ‘&’

background-processdaemonforkprocessshell

What are the practical differences from a sysadmin point of view when deploying services on a unix based system?

Best Answer

The traditional way of daemonizing is:

fork()
setsid()
close(0) /* and /dev/null as fd 0, 1 and 2 */
close(1)
close(2)
fork()

This ensures that the process is no longer in the same process group as the terminal and thus won't be killed together with it. The IO redirection is to make output not appear on the terminal.

Related Question