全部博文(298)
分类: Python/Ruby
2012-04-20 09:23:03
Linux的守护进程(Python)
Python实现参考代码:
#! /usr/bin/env python
import os, sys
def daemon_create(): # do the UNIX double-fork magic, see Stevens' "Advanced # Programming in the UNIX Environment" for details (ISBN 0201563177) try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: writelog("fork #1 failed: %d (%s)" % (e.errno, e.strerror)) sys.exit(1) # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent, print eventual PID before writelog("Daemon PID %d" % pid ) sys.exit(0) except OSError, e: writelog("fork #2 failed: %d (%s)" % (e.errno, e.strerror)) sys.exit(1) |