For this purpose, you will create a function named _daemon. Here you will see how you can create a daemon function. The working directory of the process must change.Standard input, output, and error messages are redirected to /dev/null.All open file descriptors inherited from the parent process are closed.A new session should open by calling the setsid function, and the process should be disconnected from the terminal.For this purpose, a sub-process is forked from the init process first, and then the upper process is terminated with exit. A background running process is created with init as its parent process.This way, the system can report the received errors to the user and the process will be terminated with an appropriate error code. Initial operations, such as reading configuration files or obtaining necessary system resources, must be performed before the process becomes a daemon.
A Brief Introduction to How Daemons Are CreatedĪ lot of daemons run on the system and some familiar daemon examples are as follows: