Created
October 7, 2010 16:04
-
-
Save photex/615349 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a condensed snippet of making a python daemon manually. | |
# grizzled and python-daemon were simply not working as easily as | |
# I would expect, while this little bit of code works just fine. | |
# Assumes you've already used an OptionParser instance to get | |
# some info but that's easily modified. | |
if options.daemon: | |
# Fork once | |
pid = os.fork() | |
if pid > 0: | |
sys.stdout.write("Forking daemon process\n") | |
sys.exit(0) | |
# end if | |
os.setsid() | |
os.umask(0) | |
# Fork twice, and now we're zombie proof | |
pid = os.fork() | |
if pid > 0: | |
# write out daemon pid | |
pidfile = open(options.pidfilename, 'w') | |
pidfile.write(str(pid)) | |
pidfile.close() | |
sys.exit(0) | |
# end if | |
# end if | |
# From here on out the code is executing in the daemon process | |
# if options.daemon was True. | |
logging.info("hello from your little daemon") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment