Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.py
More file actions
Latest commit
179 lines (147 loc) · 4.62 KB
/
Copy pathdaemon.py
File metadata and controls
179 lines (147 loc) · 4.62 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
try:
importos
importsys
importtime
importatexit
importresource
fromsignalimportSIGTERM,SIGHUP
exceptStandardError, e:
importsys
print"Error while loading libraries: "
printe
sys.exit()
classDaemon(object):
"""
A generic daemon class.
Usage: subclass the Daemon class and override the run() method
"""
def__init__(self, pidfile, REDIRECTIO=True):
self.pidfile=pidfile
self.REDIRECTIO=REDIRECTIO
defdaemonize(self):
try:
pid=os.fork()
ifpid>0:
# exit first parent
time.sleep(.2)
os._exit(0)
exceptOSError, e:
sys.stderr.write("fork #1 failed: %d (%s)"% (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
self.decouple()
os.setsid()
# do second fork
try:
pid=os.fork()
ifpid>0:
# exit from second parent, print eventual PID before
self.log("Daemon PID %d"%pid)
sys.stdout.flush()
os._exit(0)
exceptOSError, e:
sys.stderr.write("fork #2 failed: %d (%s)"% (e.errno, e.strerror))
os._exit(1)
sys.stdout.flush()
sys.stderr.flush()
## Cleanup
ifself.REDIRECTIO:
maxfd=resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd==resource.RLIM_INFINITY):
maxfd=1024
# close all file descriptors.
forfdinrange(0, maxfd):
try:
os.close(fd)
exceptOSError: # ERROR, fd wasn't open to begin with (ignored)
pass
os.open(os.devnull, os.O_RDWR) # standard input (0)
# Duplicate standard input to standard output and standard error.
os.dup2(0, 1) # standard output (1)
os.dup2(0, 2) # standard error (2)
self.writepid()
defwritepid(self):
pid=str(os.getpid())
file(self.pidfile,'w+').write("%s\n"%pid)
def__shutdown(self):
self.shutdown()
self.delpid()
defisdaemon(self):
"""Overried with isdaemon"""
defdelpid(self):
try:
os.remove(self.pidfile)
exceptOSError:
pass
defshutdown(self):
"""Override with shutdown"""
defreadpidfile(self):
try:
pf=file(self.pidfile,'r')
pid=int(pf.read().strip())
pf.close()
exceptIOError:
pid=None
returnpid
deflogrotate(self):
pid=self.readpidfile()
ifnotpid:
message="pidfile %s does not exist. Daemon not running?\n"
sys.stderr.write(message%self.pidfile)
return#
os.kill(pid,SIGHUP)
defstop(self):
pid=self.readpidfile()
ifnotpid:
message="pidfile %s does not exist. Daemon not running?\n"
sys.stderr.write(message%self.pidfile)
return# not an error in a restart
try:
while1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
exceptOSError, err:
err=str(err)
iferr.find("No such process") >0:
ifos.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
printstr(err)
sys.exit(1)
defrestart(self):
self.stop()
self.start()
defdecouple(self):
os.chdir("/")
os.setsid()
os.umask(0)
defstart(self,rundaemon=True):
try:
pf=file(self.pidfile,'r')
pid=int(pf.read().strip())
pf.close()
exceptIOError:
pid=None
ifpid:
try:
procfile=file("/proc/%d/status"%pid, 'r')
procfile.close()
exceptIOError:
self.delpid()
exceptTypeError:
self.delpid()
else:
message="pidfile %s already exist. Daemon already running?\n"
sys.stderr.write(message%self.pidfile)
sys.exit(1)
ifrundaemon:
self.daemonize()
else:
self.writepid()
self.decouple()
atexit.register(self.__shutdown)
self.isdaemon()
self.run()
defrun(self):
"""Override while subclassing"""