|
#!/usr/bin/python |
|
|
|
import os |
|
|
|
def daemonize(): |
|
# See http://www.faqs.org/faqs/unix-faq/programmer/faq/ - Section 1.7 |
|
print('--- %s: daemonizing' % os.getpid()) |
|
if os.fork(): # launch child and... |
|
print('--- %s: kill parent 1' % os.getpid()) |
|
os._exit(0) # kill off parent |
|
print('--- %s: old sid: %r' % (os.getpid(), os.getsid(os.getpid()))) |
|
os.setsid() |
|
print('--- %s: new sid: %r' % (os.getpid(), os.getsid(os.getpid()))) |
|
if os.fork(): # launch child and... |
|
print('--- %s: kill parent 2' % os.getpid()) |
|
os._exit(0) # kill off parent again. |
|
|
|
# File descriptor redirection intentionally left out so that we continue to see logging on stdout. |
|
# The failure case doesn't make it here anyway. |
|
|
|
print('--- %s: daemonizing done' % os.getpid()) |
|
|
|
if __name__ == "__main__": |
|
print 'starting as %d' % os.getpid() |
|
daemonize() |
|
print 'stopping as %s' % os.getpid() |