Daemon: It is usually defined as a background process, and it does not belong to any Terminal session (Terminal sessions). Many system services are implemented by daemons, such as network services, printing, and so on. The following article gives you a sample of how Python implements the daemon, and the friends you need can refer to it.
Scene Settings:
You write a Python service program and start at the command line, and your command lines are controlled by the terminal, and the Python service becomes a subprocess of the terminal program. So if you close the terminal, the command-line program will also close.
To keep your Python service from being affected by the terminal system, you need to turn it into a daemon.
A daemon is a daemon program, a program that executes in the background of a system that is independent of the control terminal and performs some periodic tasks or triggering events, usually named "D" to end, such as Common httpd, SYSLOGD, Systemd, and Dockerd.
Code implementation
Python can implement the daemon very concisely, with the code and the corresponding comments given below:
# coding=utf8import Osimport sysimport atexitdef daemonize (pid_file=none): "" "Create Daemon:p Aram Pid_file: Save the Process ID file: return: "" "# Fork a subprocess from the parent process out PID = Os.fork () # The PID of the subprocess must be 0, the parent process is greater than 0 if PID: # exits the parent process, and the Sys.exit () method performs some refresh buffering work more than the Os._exit () method Sys.exit (0) # The child process inherits the parent process's working directory by default, preferably to the root directory, or back to the file system uninstall Os.chdir ('/') # The child process inherits the parent process's umask (file permission mask) by default, and resets to 0 (Full control) to avoid affecting the program to read and write files Os.umask (0) # Let the child process become a new conversation leader and process leader Os.setsid () # Note that here is the 2nd time Fork, which is the child process of the subprocess, we call it as grandson process _pid = Os.fork () If _pid: # exit Subprocess sys.exit (0) # At this point, the grandson process is already a daemon, and then redirect the standard input, output, error descriptor (is redirected instead of closed, so that the program can avoid errors in print) # Flush buffer First, be careful to make the ship Sys.stdout.flush () The Sys.stderr.flush () # dup2 function atomically closes and copies the file descriptor, redirecting to/dev/nul, which discards all input outputs with open ('/dev/null ') as Read_null, open ('/dev/ Null ', ' W ') as Write_null:os.dup2 (Read_null.fileno (), Sys.stdin.fileno ()) os.dup2 (Write_null.fileno (), Sys.stdout.fileno ()) os.dup2 (Write_null.fileno ()) (Sys.stderr.fileno ()) # write PID file if Pid_file:with open (pid_file, ' w+ ') As F:f.write (str (os.getpid ())) # Registers the Exit function, removing the PID file Atexit.register when the process exits abnormally (oS.remove, Pid_file)
Summarize the steps to write the daemon:
Fork out child process, exit parent process
Child process Change Working directory (chdir), file permission mask (umask), process group, and session group (SETSID)
Child process fork grandson Process, exit child process
Grandson process Refresh buffer, redirect standard input/output/error (General to/dev/null, meaning discard)
(optional) PID write file
Understand a few points
Why Fork two times
The first fork is to disengage from the claws of the terminal control. The parent process exits because it sends a signal when the terminal strikes the keyboard, or when it is closed, and the forked child process becomes an orphan process after the parent process commits suicide and is then taken over by the operating system's init process, thus leaving the terminal control.
So in fact, the second fork is not necessary (many of the code in the Open source project will not fork two times). Just for the sake of careful consideration, prevent the process from opening a control terminal again. Because the child process is now the conversation leader (the first session of the conversation), have the ability to open the control terminal, and then fork again, grandson process can not open the control terminal.
File descriptor
Linux is "All Files", and file descriptors are indexes created by the kernel for open files, usually non-negative integers. The process performs IO operations through file descriptors.
By default, 0 represents standard input, 1 represents standard output, and 2 represents a standard error.
Umask Permission Mask
We know that in Linux, any file has three usage permissions for read (read), write, and execute (execute). Where the Read permission is represented by the number 4, the Write permission is 2, and the execution permission is 1. The command ls-l can view file permissions, r/w/x respectively, with read/write/execute permissions.
Any file, there are three kinds of identity permissions for users (user), user groups (group), and other groups (Others). Typically 3 numbers indicate file permissions, such as 754:
7, is user permission, that is, file owner permissions
5, is the group permission, the owner of the user group members have the permissions
4, is others permissions, that is, the permissions of other groups of users
The umask is to control the default permissions and prevent new files or folders from having full ownership.
The system generally defaults to 022 (viewed with command umask), which means that the default file creation permission is 644 and the folder is 755. You should be able to see the rules, that is, the file permissions and umask add up to 666 (laughter), folder permissions and umask Add the result is 777.
Process Group
Each process belongs to a process group (pg,process Group), and the process group can contain multiple processes.
The process group has a process leader (Leader), the ID of the process leader (PID, process ID) as the ID of the entire process group (pgid,process groupd ID).
Conversation Group
When a terminal is logged in, a session is created, and multiple process groups can be included in a session. And the process of creating a session is the conversation leader.
The session leader is already a process, and the Setsid () method can no longer be called to create the session. Thus, in the code above, the child process can call Setsid (), and the parent process cannot, because it is itself the conversation leader.
In addition, the SH (Bourne shell) does not support the session mechanism because the session mechanism requires the Shell to support work control.
Daemons and daemon processes
With the & symbol, commands can be placed in the background to execute. It is different from the daemon:
The daemon is not a terminal, it is an orphan process adopted by the INIT process, and the parent process of the background process is the terminal, which can still be printed at the terminal.
The daemon is still strong while shutting down the terminal, and the background process will stop with the user exiting unless Nohup is added
The daemon changes the session, the process group, the working directory, and the file descriptor, and the background process inherits directly from the parent process (shell)
In other words: The Guardian process is a silent struggle to fight for the youth, and then the process is silently inherit the wealth of the second generation of father.