1
# daemon code from ASPN
6
def daemonize(pidfile, home):
8
Detach this process from the controlling terminal and run it in the
9
background as a daemon.
14
REDIRECT_TO = getattr(os, 'devnull', '/dev/null')
20
raise Exception("%s [%d]" % (e.strerror, e.errno))
22
if (pid == 0): # The first child.
26
pid = os.fork() # Fork a second child.
28
raise Exception, "%s [%d]" % (e.strerror, e.errno)
30
if (pid == 0): # The second child.
34
os._exit(0) # Exit parent (the first child) of the second child.
36
os._exit(0) # Exit parent of the first child.
38
#import resource # Resource usage information.
39
#maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
40
#if (maxfd == resource.RLIM_INFINITY):
43
# Iterate through and close all file descriptors.
44
#for fd in range(3, maxfd):
47
# except OSError: # ERROR, fd wasn't open to begin with (ignored)
50
# This call to open is guaranteed to return the lowest file descriptor,
51
# which will be 0 (stdin), since it was closed above.
52
#os.open(REDIRECT_TO, os.O_RDWR) # standard input (0)
54
# Duplicate standard input to standard output and standard error.
55
#os.dup2(0, 1) # standard output (1)
56
#os.dup2(0, 2) # standard error (2)
58
f = open(pidfile, 'w')
59
f.write('%d\n' % (os.getpid(),))
60
f.write('%s\n' % (home,))
64
def is_running(pidfile):
66
f = open(pidfile, 'r')
69
pid = int(f.readline())