Use Python to write Linux daemon instances and python daemon instances
Daemon is a computer program executed in the background in UNIX or other multitasking operating systems and is not directly controlled by computer users. Such programs are initialized as processes. Generally, the daemon does not have any parent process (PPID = 1) and is directly located under init at the UNIX system process level. The daemon program usually uses the following method to make itself a daemon: Call fork for a sub-process and terminate its parent process immediately so that the sub-process can run under init. -Wikipedia
A daemon is different from a process that runs after a common user logs on to the system. It is directly initialized by the system and has nothing to do with the System user. The process enabled by the user depends on the terminal connected to the user, when the terminal exits or is disconnected, the process also ends.
Let's take a look at the process status of my Linux testing machine:
[root@home tmp]# ping www.baidu.com > /dev/null &[1] 2759[root@home tmp]# pstree -psystemd(1)-+-agetty(157) |-agetty(163) |-avahi-daemon(129)---avahi-daemon(134) |-avahi-dnsconfd(125) |-crond(121) |-dbus-daemon(130) |-haveged(128) |-ifplugd(126) |-nginx(226)---nginx(227) |-ntpd(223) |-python(2727) |-rngd(124) |-sshd(216)---sshd(2683)---bash(2690)-+-ping(2759) | `-pstree(2760) |-systemd(2687)---(sd-pam)(2688) |-systemd-journal(76) |-systemd-logind(127) |-systemd-udevd(89) `-wpa_supplicant(153)
As you can see, there is currently a ping program running in the background. If you disconnect the connection and log on again, the ping program has been terminated. That is to say, a common process is related to a user session. How can we compile a process that is irrelevant to the user session and has been running in the background? You may have noticed that python whose pid is 2727 is used above. If you just open python normally, it should be running in bash, but it runs directly in systemd. In fact, it is a daemon. Let's take a look at the simple implementation of the linux daemon written in python:
#!/usr/bin/env pythonimport osimport signalimport timelogfile="/tmp/daemon.log"pid=os.fork()#exit parent processif pid: exit()#get the pid of subprocessdaeid=os.getpid()os.setsid()os.umask(0)os.chdir("/")#Redirection file descriptorfd=open("/dev/null","a+")os.dup2(fd.fileno(),0)os.dup2(fd.fileno(),1)os.dup2(fd.fileno(),2)fd.close()log=open(logfile,'a')log.write('Daemon start up at %s\n'%(time.strftime('%Y:%m:%d',time.localtime(time.time()))))log.close()def reload(a,b): log=open(logfile,'a') log.write('Daemon reload at %s\n'%(time.strftime('%Y:%m:%d',time.localtime(time.time())))) log.close()while True: signal.signal(signal.SIGHUP,reload) time.sleep(2)
The main point is that in linux, when the parent process of a process is terminated, the system will take over the process and make init the parent process of the process. At this time, the process becomes a daemon process. You must note that you can use setsid, umask, and chdir to set the working directory, disable the file descriptor, and set the file creation mask. Save the above Code, grant the running permission, and open it in python. A new daemon is running and can process the SIGHUP signal sent by the system.
The above program is only used for testing and can only process the SIGHUP signal of the system. Use kill pid to end the process.