Created
June 10, 2012 07:53
-
-
Save yosida95/2904424 to your computer and use it in GitHub Desktop.
This file contains 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
#-*- 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