Daemon: Usually defined as a background process, and it does not belong to any terminal session (terminal session). Many system services are implemented by daemons; such as network services, printing, etc. The following article shares with you an example of how Python implements a daemon process. Friends in need can refer to it.
Scenario setting:
You wrote a python service program and started it from the command line, and your command line session was Controlled by the terminal, the python service becomes a sub-process of the terminal program. So if you close the terminal, the command line program will also close.
To make your python service permanent in the system without being affected by the terminal, you need to turn it into a daemon process.
The daemon process is a Daemon program, which is a program executed in the background of the system. It is independent of the control terminal and performs some periodic tasks or triggers events. It is usually named after the letter "d", such as Common httpd, syslogd, systemd and dockerd, etc.
Code implementation
Python can implement the daemon process very simply. The code and corresponding comments are given below:
# coding=utf8 import os import sys import atexit def daemonize(pid_file=None): """ 创建守护进程 :param pid_file: 保存进程id的文件 :return: """ # 从父进程fork一个子进程出来 pid = os.fork() # 子进程的pid一定为0,父进程大于0 if pid: # 退出父进程,sys.exit()方法比os._exit()方法会多执行一些刷新缓冲工作 sys.exit(0) # 子进程默认继承父进程的工作目录,最好是变更到根目录,否则回影响文件系统的卸载 os.chdir('/') # 子进程默认继承父进程的umask(文件权限掩码),重设为0(完全控制),以免影响程序读写文件 os.umask(0) # 让子进程成为新的会话组长和进程组长 os.setsid() # 注意了,这里是第2次fork,也就是子进程的子进程,我们把它叫为孙子进程 _pid = os.fork() if _pid: # 退出子进程 sys.exit(0) # 此时,孙子进程已经是守护进程了,接下来重定向标准输入、输出、错误的描述符(是重定向而不是关闭, 这样可以避免程序在 print 的时候出错) # 刷新缓冲区先,小心使得万年船 sys.stdout.flush() sys.stderr.flush() # dup2函数原子化地关闭和复制文件描述符,重定向到/dev/nul,即丢弃所有输入输出 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()) # 写入pid文件 if pid_file: with open(pid_file, 'w+') as f: f.write(str(os.getpid())) # 注册退出函数,进程异常退出时移除pid文件 atexit.register(os.remove, pid_file)
Summarize the steps for writing a daemon process:
fork the child process, Exit the parent process
The child process changes the working directory (chdir), file permission mask (umask), process group and session group (setsid)
The child process forks the grandson process and exits the child process
The grandson process refreshes the buffer and redirects standard input/output/error (usually to /dev/null, which means discarding)
(Optional) pid writes to file
Understand several key points
Why fork twice
The first fork is to escape from the clutches of terminal control. The reason why the parent process exits is because the terminal hits the keyboard or sends a signal to it 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 init process of the operating system, so it leaves the terminal. control.
So in fact, the second fork is not necessary (the code in many open source projects does not fork twice). It's just out of caution to prevent the process from opening a control terminal again. Because the child process is now the session leader (the first process in the session) and has the ability to open the control terminal, if it forks again, the grandson process will not be able to open the control terminal.
File descriptor
Linux is "everything is a file". The file descriptor is the index created by the kernel for the open file, usually a non-negative integer. Processes perform IO operations through file descriptors.
By default, 0 represents standard input, 1 represents standard output, and 2 represents standard error.
umask permission mask
We know that in Linux, any file has three functions: read (read), write (write) and execute (execute). kind of usage rights. Among them, the read permission is represented by the number 4, the write permission is 2, and the execution permission is 1. You can check the file permissions with the command ls -l, and r/w/x means you have read/write/execute permissions respectively.
Any file also has three identity permissions: user (User), user group (Group), and other groups (Others). Generally, three numbers are used to represent file permissions, such as 754:
7 is User permission, that is, the file owner permission
5 is Group permission, which is the user group of the owner The permissions of group members
4 are Others permissions, that is, the permissions of users in other groups
And umask is to control the default permissions and prevent new files or files Clamp has full authority.
The system generally defaults to 022 (use the umask command to view), which means that the default permissions for creating files are 644 and folders are 755. You should be able to see their pattern, that is, the sum of file permissions and umask is 666 (laughing), and the sum of folder permissions and umask is 777.
Process Group
Each process belongs to a process group (PG, Process Group), and a process group can contain multiple processes.
The process group has a process leader (Leader), and the ID of the process leader (PID, Process ID) is used as the ID of the entire process group (PGID, Process Groupd ID).
Session Group
When you log in to the terminal, a session will be created. Multiple process groups can be included in one session. The process that creates a session is the session leader.
A process that is already the session leader cannot call the setsid() method to create a session. Therefore, in the above code, the child process can call setsid(), but the parent process cannot because it is the session leader.
In addition, sh (Bourne Shell) does not support the session mechanism, because the session mechanism requires the shell to support job control (Job Control).
Daemon process and background process
With the & symbol, commands can be executed in the background. It is different from the daemon process:
The daemon process has nothing to do with the terminal and is an orphan process adopted by the init process; while the parent process of the background process is the terminal, it can still print on the terminal
The daemon process remains strong when the terminal is closed; the background process will stop when the user exits, unless nohup is added
The daemon process changes the session, process group, working directory and file descriptor , the background process directly inherits the
# of the parent process (shell). In other words: the daemon process is a promising young man who silently works hard, while the background process silently inherits the father's The rich second generation with assets.
For more relevant articles on how to implement daemon processes in Python, please pay attention to the PHP Chinese website!