Created
March 23, 2016 03:10
-
-
Save HouLinwei/023044d40fefbb6e73a7 to your computer and use it in GitHub Desktop.
create a demonize process with python
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
| 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. |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's just a memo