Python Daemon (daemon) Code instance

Source: Internet
Author: User
#-*-coding:utf-8-*-import sys, OS ' fork the current process into a daemon note: If your daemon is started by inetd, don't do it! INETD has done all the things that need to be done, including redirecting standard file descriptors, the only thing that needs to be done is chdir () and Umask () "Def daemonize (stdin= '/dev/null ', stdout= '/dev/null ',  Stderr= ' Dev/null '): ' fork The current process as a daemon, redirect the standard file descriptor (directed to/dev/null by default) #Perform first fork.  Try:pid = Os.fork () if PID > 0:sys.exit (0) #first parent out except OSError, E:sys.stderr.write ("fork #1 failed: (%d)%s\n "% (E.errno, e.strerror)) Sys.exit (1) #从母体环境脱离 Os.chdir ("/") Os.umask (0) os.setsid () #执行第二次fo RK Try:pid = os.fork () if PID > 0:sys.exit (0) #second parent out except OSError, E:sys.stderr.write ( "Fork #2 failed: (%d)%s]n"% (e.errno,e.strerror)) Sys.exit (1) #进程已经是守护进程了, redirect standard file descriptor for F in Sys.stdout, Sys.stderr : F.flush () si = file (stdin, ' r ') so = file (stdout, ' A + ') se = file (stderr, ' A + ', 0) os.dup2 (Si.fileno (), Sys.stdin.filen O ()) os.dup2 (So.fileno (), Sys.stdout.fileno ()) os.dup2 (Se.fileno (), Sys.stderr.fileno ())Def _example_main (): ' Sample function: Print a number and timestamp per second ' Import time Sys.stdout.write (' Daemon started with PID%d\n '% os.getpid ()) Sys.stdout.write (' Daemon stdout output\n ') sys.stderr.write (' Daemon stderr output\n ') c = 0 while True:sys.stdout . Write ('%d:%s\n '% (c, time.ctime ())) Sys.stdout.flush () c = c+1 Time.sleep (1) if __name__ = = "__main__": Daemoni Ze () _example_main () "The first fork is for the shell to return and lets you complete the SETSID (removed from your control terminal so that you do not receive the signal unexpectedly). Setsid makes this process a "session leader", that is, if the process opens any terminal, the terminal becomes the control terminal for this process. We don't need a daemon to have any control terminals, so we fork it again. After the second fork, the process is no longer a "session leader", so it can open any file (including the terminal) and not accidentally get a control terminal again: the Umask () function creates a masking word for the process settings file mode. and return the previous value in the shell command line input: Umask know the current file mode creation screen Word common several umask values are 002,022 and 027,002 block other users from writing your files, 022 block members of the same group and other users to write your files, 027 prevent the same group members from writing your files as well as other users reading or writing your files RWX-RWX-RWX representative is 777 all of them have permission to read and write and execute chmod () Change the permission bit of the file int dup (int filedes) Returns the new file descriptor must be the smallest numeric value in the current file descriptor int dup2 (int filedes, int filedes2); The new file descriptor returned by these two functions share the same file table entry as the parameter filedes. '''
  • 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.