Skip to content

Instantly share code, notes, and snippets.

@maliubiao
Last active August 29, 2015 14:06
Show Gist options
  • Save maliubiao/cb786b9f66486a124b2e to your computer and use it in GitHub Desktop.
Save maliubiao/cb786b9f66486a124b2e to your computer and use it in GitHub Desktop.
替代难用的nohup
#! /usr/bin/env python
#chmod +x daemon.py
#complete -cf daemon.py
import sys
import os
if len(sys.argv) < 2:
print "daemon.py cmdline..."
exit(0)
log = open("/tmp/" + " ".join([k.replace("/", ".") for k in sys.argv[1:]]) + ".log",
"w+", buffering=0)
null = open("/dev/null", "r")
if not os.fork():
os.setsid()
if not os.fork():
os.dup2(null.fileno(), 0)
os.dup2(log.fileno(), 1)
os.dup2(log.fileno(), 2)
os.execvp(sys.argv[1], sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment