Skip to content

Instantly share code, notes, and snippets.

@maliubiao
Created November 20, 2014 01:35
Show Gist options
  • Save maliubiao/b59c5c7f3649c98158f5 to your computer and use it in GitHub Desktop.
Save maliubiao/b59c5c7f3649c98158f5 to your computer and use it in GitHub Desktop.
autoreload.py
#! /usr/bin/env python
#chmod +x daemon.py
#complete -cf daemon.py
import sys
import os
def daemonize(log):
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)
else:
exit()
else:
exit()
def autoreload():
while True:
if not os.fork():
os.execvp(sys.argv[1], sys.argv[1:])
else:
os.wait()
if __name__ == "__main__":
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")
daemonize(log)
autoreload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment