Python daemon and python daemon

Source: Internet
Author: User

Python daemon and python daemon

If you write a server program and press ctrl + c to exit or close the terminal, the server program will exit, so you want to make this program a daemon process like httpd, it is always running on the backend and will not be affected by the terminal.
Daemon is used for daemon. For example, httpd and mysqld. The last letter d actually indicates daemon.

To write the daemon process, follow these steps:

1. fork sub-process, and then the parent process exits. At this time, the sub-process will be taken over by the init process.
2. Modify the working directory of the sub-process, create a new process combination session, and modify umask.
3. The sub-process fork a process again. This process can be called the Sun Tzu process, and then the sub-process exits.
4. Redirect standard input stream of the Sun Tzu process, standard output stream, standard error to/dev/null
After completing the above four steps, the final Sun Tzu process is called a daemon process. Let's take a look at the code and analyze the reasons for the next step.

#! /Usr/bin/env python # coding = utf8def createDaemon (): import OS, sys, time # generate sub-processes, and the parent process exits try: pid = OS. fork () if pid> 0: sys. exit (0) failed t OSError, error: print 'fork' sys. exit (1) # modify the sub-process working directory OS. chdir ("/") # create a new session and the sub-process becomes the first process OS of the session. setsid () # modify the umask OS in the working directory. umask (0) # create a child process, and then the child process exits try: pid = OS. fork () if pid> 0: print "Daemon PID % d" % pid sys. exit (0) failed t OSError, error: print "fork" sys. exit (1) run () def ping (): import 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 sub-process. The parent process exits.
Generally, we connect to the server through the terminal when executing the server program. After successful connection, the shell environment will be loaded. The shell in the terminal box is a process, and the shell process is a sub-process of the terminal process, you can easily see through the ps command that in this shell environment, all the programs executed at the beginning are subprocesses of the shell process, which will naturally be affected by the shell process, after fork sub-process in the program, the parent process exits. For the shell process, even if the execution of this parent process is complete, the child process generated will be taken over by the init process, thus, it is out of terminal control.
2. Modify the working directory of the sub-process
When a child process is created, it inherits the working directory of the parent process. If the program to be executed is in the USB flash disk, the USB flash disk cannot be detached.
3. Create a new session
After setsid is used, the child process becomes the first process of the new session, and the child process becomes the leader process of the new process group. The child process has no control terminal.
4. Modify umask
Umask shields permissions and sets all permissions to 0, which can avoid permission issues when reading and writing files.
5. fork grandson process, sub-process exited
After the above steps, the sub-process will become the new process group leader, you can apply to open the terminal again, in order to avoid this problem, fork grandson process,
6. Redirect standard input stream of the Sun Tzu process, standard output stream, standard error stream to/dev/null
Because it is a daemon process that is out of the terminal, the standard input stream, the standard input stream, and the standard error stream have no meaning, so they all turn to/dev/null, which means to discard

Let's run a program to see the effect.

It can be seen that this script program has been put into the background and can only be ended using the killall method,
Next, let's take a look at the recorded logs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.