Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

python daemonize usage example.

  • Loading branch information...
commit 4421b383572b00d3da6e298b2e86514cca06503e 1 parent 8e42c9a
@soarpenguin authored
Showing with 37 additions and 0 deletions.
  1. +37 −0 bashrc/daemonize.py
View
37 bashrc/daemonize.py
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+
+import errno
+import fcntl
+import os
+import random
+import re
+import signal
+import socket
+import sys
+import threading
+import time
+
+
+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()
+ for fd in xrange(3, 1024):
+ try:
+ os.close(fd)
+ except OSError: # This FD wasn't opened...
+ pass # ... ignore the exception.
+
Please sign in to comment.
Something went wrong with that request. Please try again.