-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_sample.py
264 lines (213 loc) · 7.06 KB
/
daemon_sample.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/opt/python3/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
import time
import psutil
import signal
import logging
class Daemon(object):
"""
Usage: - create your own a subclass Daemon class and override the run() method. Run() will be periodically the calling inside the infinite run loop
- you can receive reload signal from self.isReloadSignal and then you have to set back self.isReloadSignal = False
"""
def __init__(self, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.ver = 0.1 # version
# 0 means none pause between the calling of run() method.
self.pauseRunLoop = 0
# 0 means without a pause between stop and start during the restart of the daemon
self.restartPause = 1
# when terminate a process, wait until kill the process with SIGTERM signal
self.waitToHardKill = 3
self.isReloadSignal = False
self._canDaemonRun = True
self.processName = os.path.basename(sys.argv[0])
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
def _sigterm_handler(self, signum, frame):
self._canDaemonRun = False
def _reload_handler(self, signum, frame):
self.isReloadSignal = True
def _makeDaemon(self):
"""
Make a daemon, do double-fork magic.
"""
try:
pid = os.fork()
if pid > 0:
# Exit first parent.
sys.exit(0)
except OSError as e:
m = "Fork #1 failed: {}".format(e)
print(m)
sys.exit(1)
# Decouple from the parent environment.
os.chdir("/")
os.setsid()
os.umask(0)
# Do second fork.
try:
pid = os.fork()
if pid > 0:
# Exit from second parent.
sys.exit(0)
except OSError as e:
m = "Fork #2 failed: {}".format(e)
print(m)
sys.exit(1)
m = "The daemon process is going to background."
print(m)
# Redirect standard file descriptors.
sys.stdout.flush()
sys.stderr.flush()
si = open(self.stdin, 'r')
so = open(self.stdout, 'a+')
se = open(self.stderr, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def _getProces(self):
procs = []
for p in psutil.process_iter():
if self.processName in [part.split('/')[-1] for part in p.cmdline()]:
# Skip the current process
if p.pid != os.getpid():
procs.append(p)
return procs
def start(self):
"""
Start daemon.
"""
# Handle signals
signal.signal(signal.SIGINT, self._sigterm_handler)
# go to null -> si = open(os.devnull, 'r')
signal.signal(signal.SIGTERM, self._sigterm_handler)
signal.signal(signal.SIGHUP, self._reload_handler)
# Check if the daemon is already running.
procs = self._getProces()
if procs:
m = "Find a previous daemon processes with PIDs {}. Is not already the daemon running?".format(
",".join([str(p.pid) for p in procs]))
print(m)
sys.exit(1)
else:
m = "Start the daemon version {}".format(self.ver)
print(m)
# Daemonize the main process
self._makeDaemon()
# Start a infinitive loop that periodically runs run() method
self._infiniteLoop()
def version(self):
print("The daemon version {}".format(self.ver))
def status(self):
"""
Get status of the daemon.
"""
procs = self._getProces()
if procs:
m = "The daemon is running with PID {}.".format(
",".join([str(p.pid) for p in procs]))
print(m)
else:
m = "The daemon is not running!"
print(m)
def reload(self):
"""
Reload the daemon.
"""
procs = self._getProces()
if procs:
for p in procs:
os.kill(p.pid, signal.SIGHUP)
m = "Send SIGHUP signal into the daemon process with PID {}.".format(
p.pid)
print(m)
else:
m = "The daemon is not running!"
print(m)
def stop(self):
"""
Stop the daemon.
"""
procs = self._getProces()
def on_terminate(process):
m = "The daemon process with PID {} has ended correctly.".format(
process.pid)
print(m)
if procs:
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(
procs, timeout=self.waitToHardKill, callback=on_terminate)
for p in alive:
m = "The daemon process with PID {} was killed with SIGTERM!".format(
p.pid)
print(m)
p.kill()
else:
m = "Cannot find some daemon process, I will do nothing."
print(m)
def restart(self):
"""
Restart the daemon.
"""
self.stop()
if self.restartPause:
time.sleep(self.restartPause)
self.start()
def _infiniteLoop(self):
try:
if self.pauseRunLoop:
time.sleep(self.pauseRunLoop)
while self._canDaemonRun:
self.run()
time.sleep(self.pauseRunLoop)
else:
while self._canDaemonRun:
self.run()
except Exception as e:
m = "Run method failed: {}".format(e)
sys.stderr.write(m)
sys.exit(1)
# this method you have to override
def run(self):
pass
########################################################################################################################
# example of a custom run method
class MyDaemon(Daemon):
def run(self):
x = 10
y = x ** 10
return y
def main(self):
a = run.y()
print(a)
########################################################################################################################
if __name__ == "__main__":
daemon = MyDaemon()
daemon.main()
usageMessage = "Usage: {} (start|stop|restart|status|reload|version)".format(
sys.argv[0])
if len(sys.argv) == 2:
choice = sys.argv[1]
if choice == "start":
daemon.start()
elif choice == "stop":
daemon.stop()
elif choice == "restart":
daemon.restart()
elif choice == "status":
daemon.status()
elif choice == "reload":
daemon.reload()
elif choice == "version":
daemon.version()
else:
print("Unknown command.")
print(usageMessage)
sys.exit(1)
sys.exit(0)
else:
print(usageMessage)
sys.exit(1)