What is a daemon process? In fact, there is no clear definition of the daemon process, but there are some characteristics of the daemon, which is what it needs to follow.
The first feature of a daemon is a program that runs in the background for a long time, and is primarily designed to provide a service, and in order for the service to be available whenever possible, the service is required to be running, and the daemon is guarding the service from hanging. Common daemons in Linux typically end with D, such as Apache's Httpd,samba smbd,ssh sshd.
Its second feature is the environment isolation from the process that initiated it, including closing all file descriptors, terminals, sessions, process groups, certain environment variables (such as working directory), and file masks.
Why get out of the terminal?
If it is not out of the terminal, then it is possible to receive signals from the terminal, such as SIGINT (CTRL + C, will be sent to all foreground process groups, its default behavior is to end the process), SIGHUP (will be sent to the session first process).
Why would you want to close all the file descriptors it opened?
If the process of creating it opens a file and then creates the daemon, the process inherits the FD, and if the daemon does not close the FD, one will take up resources, two I guess and change the working directory, if the file is located in the Mount directory, then it cannot be umount.
Why should I clear the mask?
This is because assuming that its parent process has made a special provision to the mask before, the daemon inherits this as a child process, and when it creates the file, the file's permissions may be inconsistent with the actual settings because of the mask.
Third feature, daemons are typically started by a startup script
From the Apue Daemon Chapter example, a process daemon code:
-----
Or use a Linux ready-made function, daemon.
The daemon under Linux daemon