def run(program, *args): pid = os.fork() if pid==0: os.execvp(program, (program,) + args) return os.wait() run("ls","-l")
建立守護進程 python
def damon_init(): try: pid = os.fork() if pid > 0: sys.exit(0) except OSError,e: sys.stderr.write("fork #1 failed") sys.exit(1) os.chdir("/") os.setsid() os.umask(0) try: pid = os.fork() if pid > 0: sys.exit(0) except OSError,e: sys.stderr.write("fork #2 failed") sys.exit(1)