Skip to content

Instantly share code, notes, and snippets.

@HouLinwei
Created March 23, 2016 03:10
Show Gist options
  • Select an option

  • Save HouLinwei/023044d40fefbb6e73a7 to your computer and use it in GitHub Desktop.

Select an option

Save HouLinwei/023044d40fefbb6e73a7 to your computer and use it in GitHub Desktop.
create a demonize process with python
def daemonize():
"""Performs the necessary dance to become a background daemon."""
if os.fork():
os._exit(0)
os.chdir("/")
os.umask(022)
os.setsid()
os.umask(0)
if os.fork():
os._exit(0)
stdin = open(os.devnull)
stdout = open(os.devnull, 'w')
os.dup2(stdin.fileno(), 0)
os.dup2(stdout.fileno(), 1)
os.dup2(stdout.fileno(), 2)
stdin.close()
stdout.close()
os.umask(022)
for fd in xrange(3, 1024):
try:
os.close(fd)
except OSError: # This FD wasn't opened...
pass # ... ignore the exception.
@HouLinwei
Copy link
Copy Markdown
Author

it's just a memo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment