def _daemonize(self):
# double-fork. refer to "Advanced Programming in the UNIX Environment"
try: # first fork
pid = os.fork()
if pid > 0: # first parent
os.waitpid(pid, 0) # wait for second child to start
return False # return to caller of daemonize()
except OSError, e:
self.log('fork #1 failed: %s' % e)
return # return caller of daemonize()
# decouple first parent
os.setsid()
os.chdir("/")
os.umask(0)
ppid = os.getpid() # yes, getpid(). it will be the child's ppid
try: # second fork
self._pid = os.fork()
if self._pid > 0: # second parent. just exit
os._exit(0) # this is the wait() above
except OSError, e:
self.log('fork #2 failed: %s' % e)
os._exit(1)
# wait until ppid changes
while os.getppid() == ppid:
time.sleep(0.1)
return True
评论列表
文章目录