A service-side program was written to open the terminal to run the program directly, but the terminal was limited. If CTRL + C exits or shuts down the terminal, the server program exits. So I want to make this server daemon process, like httpd, VSFTPD, mysqld, always running in the back end, not affected by the terminal.
Guardian process English for daemon, like httpd, Mysqld, vsftpd the last letter D is actually means daemon.
Steps to write the daemon:
Fork the child process, and then the parent process exits, at which point the child process is taken over by the INIT process. Modify the working directory of the child process, create a new process group, and modify the umask.
The child process again fork a process, which can be called the grandson process, and then the child process exits. Redirect the grandchild process's standard input stream, standard output stream, and standard error stream to/dev/null.
Complete the 4 steps above, then the ultimate grandson process is called the daemon. Look at the code first, and then analyze the reason for each step later.
The above program does not have any error handling, but does not affect the principle analysis. It needs to be perfected if it is to be applied to the project. The following author discusses their own understanding of each step.
1, fork child process, parent process exit
Usually, when we execute the service-side program will be connected to the server through the terminal, the successful connection will load the shell environment, terminal and shell are processes, shell process is a child process of the terminal process, through the PS command can be easily viewed. The program that executes at the beginning of the shell environment is a child of the shell process and is naturally affected by the shell process. After the child process is fork in the program, the parent process exits, and for the shell process, the parent process is executed, and the resulting subprocess is taken over by the Init process and thus out of control of the terminal.
2, modify the working directory of the child process
The child process inherits the working directory of the parent process when it is created, and if the executed program is in the U disk, it will cause the U disk not to unload.
3. Create a new session
With Setsid, the child process becomes the first session of the new session (session leader), and the child process becomes the lead process for the group, and the child process does not control the terminal.
4, modify the Umask
Because Umask will block permissions, it is set to 0, which avoids permission problems when reading and writing files.
5, fork grandson process, child process exit
After a few steps above, the child process will become the new process group boss, can re-apply to open the terminal, in order to avoid this problem, fork grandson process out.
6. REDIRECT Grandson process standard input stream, standard output stream, standard error stream to/dev/null
Because it is the daemon, itself is out of the terminal, then the standard input stream, standard output stream, standard error stream is meaningless. So all turned to/dev/null, that is, the meaning of discarded.
Python Deamon Example