1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# daemon code from ASPN
#
import os
def daemonize(pidfile, home):
"""
Detach this process from the controlling terminal and run it in the
background as a daemon.
"""
WORKDIR = "/"
REDIRECT_TO = getattr(os, 'devnull', '/dev/null')
MAXFD = 1024
try:
pid = os.fork()
except OSError, e:
raise Exception("%s [%d]" % (e.strerror, e.errno))
if pid == 0: # The first child.
os.setsid()
try:
pid = os.fork() # Fork a second child.
except OSError, e:
raise Exception("%s [%d]" % (e.strerror, e.errno))
if pid == 0: # The second child.
os.chdir(WORKDIR)
else:
os._exit(0) # Exit parent (the first child) of the second child.
else:
os._exit(0) # Exit parent of the first child.
import resource # Resource usage information.
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd == resource.RLIM_INFINITY):
maxfd = MAXFD
fd = os.open(REDIRECT_TO, os.O_RDONLY)
os.dup2(fd, 0)
fd = os.open(REDIRECT_TO, os.O_WRONLY)
os.dup2(fd, 1)
os.dup2(fd, 2)
# Iterate through and close all other file descriptors.
for fd in range(3, maxfd):
try:
os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass
f = open(pidfile, 'w')
f.write('%d\n' % (os.getpid(),))
f.close()
def is_running(pidfile):
try:
f = open(pidfile, 'r')
except IOError:
return False
pid = int(f.readline())
f.close()
try:
os.kill(pid, 0)
except OSError:
# no such process
return False
return True
|