def daemonize(self):
try:<EOL><INDENT>pid = os.fork()<EOL>if pid > <NUM_LIT:0>:<EOL><INDENT>sys.exit(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>except OSError as err:<EOL><INDENT>sys.stderr.write(<EOL>"<STR_LIT>" % (err.errno, err.strerror))<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>os.chdir("<STR_LIT:/>")<EOL>os.setsid()<EOL>os.umask(<NUM_LIT:0>)<EOL>try:<EOL><INDENT>pid = os.fork()<EOL>if pid > <NUM_LIT:0>:<EOL><INDENT>sys.exit(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>except OSError as err:<EOL><INDENT>sys.stderr.write(<EOL>"<STR_LIT>" % (err.errno, err.strerror))<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>si = file(self.stdin, '<STR_LIT:r>')<EOL>so = file(self.stdout, '<STR_LIT>')<EOL>se = file(self.stderr, '<STR_LIT>', <NUM_LIT:0>)<EOL>os.dup2(si.fileno(), sys.stdin.fileno())<EOL>os.dup2(so.fileno(), sys.stdout.fileno())<EOL>os.dup2(se.fileno(), sys.stderr.fileno())<EOL>atexit.register(self.delpid)<EOL>pid = str(os.getpid())<EOL>file(self.pidfile, '<STR_LIT>').write("<STR_LIT>" % pid)<EOL>
Do the UNIX double-fork magic

        See Stevens' "Advanced Programming in the UNIX Environment" for
        details (ISBN 0201563177)
        http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16