1 # This code seems to be in public domain
2 # http://www.google.com/search?client=opera&rls=en&q=unix+daemon+double+fork&sourceid=opera&ie=utf-8&oe=utf-8
8 # do the UNIX double-fork magic, see Stevens' "Advanced
9 # Programming in the UNIX Environment" for details (ISBN 0201563177)
16 print >>sys
.stderr
, "fork #1 failed: %d (%s)" % (e
.errno
, e
.strerror
)
19 # decouple from parent environment
28 # exit from second parent, print eventual PID before
29 print "Daemon PID %d" % pid
32 print >>sys
.stderr
, "fork #2 failed: %d (%s)" % (e
.errno
, e
.strerror
)
35 #TODO - move standard output to log file/library
36 # -there should be more here - see comments on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
37 # Redirect standard file descriptors
38 sys
.stdin
= open('/dev/null', 'r')
39 sys
.stdout
= open('/dev/null', 'w')
40 sys
.stderr
= open('/dev/null', 'w')