Skip to content

Instantly share code, notes, and snippets.

@yosida95
Created June 10, 2012 07:53
Show Gist options
  • Save yosida95/2904424 to your computer and use it in GitHub Desktop.
Save yosida95/2904424 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
import os
import sys
def daemonize(target=None, args=(), kwargs={}):
# Quit Process Group Leader
try:
if os.fork() > 0:
sys.exit(0)
except OSError:
sys.exit(1)
# Create New Session
try:
os.setsid()
except:
sys.exit(1)
# Create Daemon Process
try:
if os.fork() > 0:
sys.exit(0)
except OSError:
sys.exit(1)
os.chdir('/')
os.umask(0)
sys.stdin = open('/dev/null', 'r')
sys.stdout = open('/dev/null', 'w')
sys.stderr = open('/dev/null', 'w')
target(*args, **kwargs)
def something():
# something
if __name__ == '__main__':
daemonize(target=something)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment