Skip to content

Instantly share code, notes, and snippets.

@photex
Created October 7, 2010 16:04
Show Gist options
  • Save photex/615349 to your computer and use it in GitHub Desktop.
Save photex/615349 to your computer and use it in GitHub Desktop.
# 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