Home > Article > Backend Development > python daemon daemon implementation
If you write a server-side program, if you exit with ctrl+c or close the terminal, the server-side program will exit, so you want to make this program a daemon process, like httpd, always running on the backend, which will not Affected by terminal.
The daemon process is called daemon in English, such as httpd and mysqld. The last letter d actually means daemon.
Steps to write the daemon process:
1. Fork the child process, and then the parent process exits. At this time, the child process will be taken over by the init process.
2. Modify the working directory of the child process, create a new process combination and new session, and modify the umask.
3. The child process forks a process again. This process can be called a grandson process, and then the child process exits.
4. Redirect the standard input stream, standard output stream, and standard error of the grandchild process to /dev/null
Complete the above 4 steps, then the final grandson process is called a daemon process. Let’s look at the code first. , analyze the reasons for the next step later.
#!/usr/bin/env python #coding=utf8 def createDaemon(): import os, sys, time #产生子进程,而后父进程退出 try: pid = os.fork() if pid > 0:sys.exit(0) except OSError,error: print 'fork' sys.exit(1) #修改子进程工作目录 os.chdir("/") #创建新的会话,子进程成为会话的首进程 os.setsid() #修改工作目录的umask os.umask(0) #创建孙子进程,而后子进程退出 try: pid = os.fork() if pid > 0: print "Daemon PID %d"%pid sys.exit(0) except OSError,error: print "fork" sys.exit(1) run() def ping(): import os os.system('ping www.baidu.com >/dev/nul') def run(): while True: import time,threading fd = open('/home/ping.log', 'a') fd.write("start time---------:%s\n"%time.ctime()) fd.flush() t=threading.Thread(target=ping,args=()) t.start() time.sleep(3) fd.write("end of time--------:%s\n"%time.ctime()) fd.flush() fd.close() if __name__=='__main__': createDaemon()
1. Fork the child process and the parent process exits
Usually, when we execute the server program, we will connect to the server through the terminal. After a successful connection The shell environment will be loaded. Both the terminal and the shell are processes. The shell process is the child process of the terminal process. You can easily see it through the ps command. The programs that are started to be executed in this shell environment are all child processes of the shell process. Naturally It will be affected by the shell process. After forking the child process in the program, the parent process exits. For the shell process, even if the parent process is completed, the generated child process will be taken over by the init process, thus breaking away from the terminal control. .
2. Modify the working directory of the child process
When the child process is created, it will inherit the working directory of the parent process. If the executed program is in the U disk, the U disk cannot be uninstalled.
3. Create a new session
After using setsid, the child process will become the first process of the new session, the child process will become the leader process of the new process group, and the child process will not control the terminal.
4. Modify umask
Since umask will block permissions, all settings are 0, which can avoid permission problems when reading and writing files
5. Fork the grandson process, and the child process exits
After the above After a few steps, the child process will become the new process group boss and can reapply to open the terminal. In order to avoid this problem, fork the grandson process processing,
6. Redirect the standard input stream, standard output stream, and standard of the grandchild process. The error flows to /dev/null
Because it is a daemon process and has been separated from the terminal, then the standard input stream, standard input stream, and standard error stream are meaningless, so they are all redirected to /dev/null, which is discarded. Meaning
Let’s run this program and see the effect
As you can see from the picture above, this script program has been put into the background and can only be used Killall method to end it,
Next let’s take a look at the recorded log
For more articles related to python daemon daemon implementation, please pay attention to the PHP Chinese website!