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.
13
REDIRECT_TO = getattr(os, 'devnull', '/dev/null')
19
raise Exception("%s [%d]" % (e.strerror, e.errno))
21
if pid == 0: # The first child.
25
pid = os.fork() # Fork a second child.
27
raise Exception, "%s [%d]" % (e.strerror, e.errno)
29
if pid == 0: # The second child.
32
os._exit(0) # Exit parent (the first child) of the second child.
34
os._exit(0) # Exit parent of the first child.
36
import resource # Resource usage information.
37
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
38
if (maxfd == resource.RLIM_INFINITY):
41
fd = os.open(REDIRECT_TO, os.O_RDONLY)
43
fd = os.open(REDIRECT_TO, os.O_WRONLY)
47
# Iterate through and close all other file descriptors.
48
for fd in range(3, maxfd):
51
except OSError: # ERROR, fd wasn't open to begin with (ignored)
54
f = open(pidfile, 'w')
55
f.write('%d\n' % (os.getpid(),))
56
f.write('%s\n' % (home,))
60
def is_running(pidfile):
62
f = open(pidfile, 'r')
65
pid = int(f.readline())