#-*-coding:utf-8-*-Import sys, OS ' fork the current process as a daemon Note: If your daemon is started by inetd, don't do it! INETD completes all the things that need to be done, including redirecting the standard file descriptor, what needs to be done is only chdir () and Umask () ' Def daemonize (stdin= '/dev/null ', stdout= '/dev/null ', St
Derr= ' Dev/null '): ' fork The current process is a daemon, redirecting the standard file descriptor (directed to/dev/null by default) ' #Perform A-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 (
) #执行第二次fork 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, ' + ') se = file (stderr, ' + ', 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 per second ' Import time Sys.stdout.wri Te (' Daemon started with PID%d\n '% os.getpid ()) sys.stdout.write (' Daemon stdout output\n ') sys.stderr.write (' Daemon s Tderr 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 allow the shell to return while allowing you to complete setsid (from your The control terminal is removed so that the signal is not accidentally received. Setsid makes this process a "session leader", that is, if the process opens any terminal, the terminal becomes the control terminal for the process. We don't need a daemon to have any control terminals, so we fork once again.
After the second fork, the process is no longer a "session leader", so that it can open any file (including terminals) without accidentally getting another control terminal again: the Umask () function creates a masked word for the process Setup file mode and returns the previous value At the shell command line input: Umask It is known that the current file mode creation screen Word common several umask values are 002,022 and 027,002 to prevent other users from writing your files, 022 to prevent the same group of members and other users write your files, 027 block the same group of members to write your files and other users read or write or execute your files RWX-RWX-RWX representative is 777 all people have permission to read and write and execute chmod () Change file permission bit int dup (int filedes)
Returns the new file descriptor must be the minimum numeric int dup2 (int filedes, int filedes2) in the current file descriptor, and the new file descriptor returned by these two functions shares the same file table entry as the parameter filedes. '''