Python daemon code instance and python daemon
#-*-Coding: UTF-8-*-import sys, OS ''' fork the current process as a daemon. Note: If your daemon is started by inetd, do not do this! Inetd completes all the things that need to be done, including redirecting standard file descriptors. Only chdir () and umask () are required () '''def daemonize (stdin = '/dev/null', stdout ='/dev/null', stderr = 'dev/null '): '''fork: the current process is a daemon, and the standard file descriptor is redirected to/dev/null by default. ''' # Perform first Fork. try: pid = OS. fork () if pid> 0: sys. exit (0) # first parent out primary T OSError, e: sys. stderr. write ("fork #1 failed: (% d) % s \ n" % (e. errno, e. strerror) sys. exit (1) # Remove the OS from the parent environment. chdir ("/") OS. umas K (0) OS. setsid () # Run the second fork try: pid = OS. fork () if pid> 0: sys. exit (0) # second parent out primary T OSError, e: sys. stderr. write ("fork #2 failed: (% d) % s] n" % (e. errno, e. strerror) sys. exit (1) # The process is already a daemon. Redirect the 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. fileno () OS. dup2 (so. fileno (), sys. stdout. Fileno () OS. dup2 (se. fileno (), sys. stderr. fileno () def _ example_main (): ''' example function: print a number and timestamp ''' import time sys per second. 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 _": daemonize () _ Example_main () ''' the first fork is to let the shell return and let you complete setsid (removed from your control terminal, so that you will not receive the signal accidentally ). Setsid makes the process a "session leader", that is, if the process opens any terminal, the terminal will become the control terminal of the process. We don't need a daemon with any control terminal, So we fork again. After the second fork, this process is no longer a "session leader", so that it can open any file (including the terminal) and will not accidentally obtain another control terminal. Additional instructions: the umask () function creates a blocking word for the process setting file mode, and returns the previous value in the shell command line input: umask can be seen in the current file mode. Common umask values are 002,022 and 027,002, which prevent other users from writing your files. 022 prevents the same group members and other users from writing your files, 027 prevent group members from writing your files and other users to read/write or execute your files. rwx-rwx indicates that 777 of all users have the permission to read and write and execute chmod () int dup (int filedes) returns the value of int dup2 (int filedes, int filedes2) in the current file descriptor ); the new file descriptor returned by these two functions shares the same file table item with the filedes parameter. '''