-
Notifications
You must be signed in to change notification settings - Fork 4
/
daemoncmd.py
executable file
·357 lines (290 loc) · 12.1 KB
/
daemoncmd.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
'''
For more information on daemons in python, see:
* http://pypi.python.org/pypi/python-daemon
* http://www.python.org/dev/peps/pep-3143/
* http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
* http://en.wikipedia.org/wiki/Daemon_(computing)
Similar implementations:
* https://github.com/indexzero/forever
This module has two separate uses cases:
* running a command as a daemon process
* forking the current process as a daemon.
Daemonizing a command allows one to start, stop, and restart a non-daemon
command as a daemon process. This requires specifying a pid file which is used
to interact with the process.
Usage examples:
python daemoncmd.py start --pidfile /tmp/daemon.pid \
--stdout /tmp/daemon.log --stderr /tmp/daemon.log sleep 100
python daemoncmd.py restart --pidfile /tmp/daemon.pid \
--stdout /tmp/daemon.log --stderr /tmp/daemon.log sleep 100
python daemoncmd.py status --pidfile /tmp/daemon.pid
python daemoncmd.py stop --pidfile /tmp/daemon.pid
Another use case is forking the current process into a daemon. According
to pep 3143, forking as a daemon might be done by the standard library some
day.
Usage example:
import daemoncmd
import mytask
daemoncmd.daemonize()
mytask.doit()
Or from the command line:
python -c 'import daemoncmd, mytask; daemoncmd.daemonize(); mytask.doit()'
Other usage notes:
* The command should not daemonize itself, since that is what this script does
and it would make the pid in the pidfile incorrect.
* The command should be refer to the absolute path of the executable, since
daemonization sets the cwd to '/'. More generally, do not assume what the
cwd is.
* If daemoncmd.py is run by monit, etc., PATH and other env vars might be
restricted for security reasons.
* daemoncmd.py does not try to run the daemon as a particular uid. That would
be handled by a process manager like monit, launchd, init, god, etc.
* When running under monit, etc., pass environment variables to the command
like so:
FOO=testing ./daemoncmd.py start --pidfile /tmp/daemon.pid \
--stdout /tmp/daemon.log printenv FOO
'''
import sys
import os
import signal
import errno
import argparse
import time
START = 'start'
STOP = 'stop'
RESTART = 'restart'
STATUS = 'status'
def start(argv, pidfile, stdin='/dev/null', stdout='/dev/null',
stderr='/dev/null'):
'''
Start a new daemon, saving its pid to pidfile.
Do not start the daemon if the pidfile exists and the pid in it is running.
'''
# use absolute path, since daemonize() changes cwd
pidfile = os.path.abspath(pidfile)
pid = getpid(pidfile)
# start process pidfile does not have pid or the pid is not a running
# process.
if pid and running(pid):
mess = "Start aborted since pid file '%s' exists" % pidfile
mess += " and pid '%s' is running.\n" % pid
sys.stderr.write(mess)
sys.exit(1)
sys.stdout.write('Starting process.\n')
daemonize_command(argv, pidfile, stdin, stdout, stderr)
def stop(pidfile):
'''
pidfile: a file containing a process id.
stop the pid in pidfile if pidfile contains a pid and it is running.
'''
# use absolute path, since daemonize() changes cwd
pidfile = os.path.abspath(pidfile)
pid = getpid(pidfile)
# stop process (if it exists)
if not pid:
sys.stderr.write(("Warning: Could not stop process because pid file "
"'%s' is missing.\n" % pidfile))
elif not running(pid):
sys.stderr.write(('Warning: pid "%s" in pid file "%s" is already not '
'running.\n' % (pid, pidfile)))
else:
sys.stdout.write('Stopping process. pid={}\n'.format(pid))
try:
os.kill(pid, signal.SIGTERM)
# a pause, so daemon will have a chance to stop before it gets restarted.
time.sleep(1)
except OSError as err:
sys.stderr.write('Failed to terminate pid "%s". Exception: %s.\n'
% (pid, err))
sys.exit(1)
def restart(argv, pidfile, stdin='/dev/null', stdout='/dev/null',
stderr='/dev/null'):
'''
stop the process in pidfile. start argv as a new daemon process. save its
pid to pidfile.
'''
stop(pidfile)
start(argv, pidfile, stdin, stdout, stderr)
def status(pidfile):
# use absolute path, since daemonize() changes cwd
pidfile = os.path.abspath(pidfile)
pid = getpid(pidfile)
if pid and running(pid):
sys.stdout.write('process running; pid={}\n'.format(pid))
else:
sys.stdout.write('process stopped\n')
def daemonize_command(argv, pidfile, stdin='/dev/null', stdout='/dev/null',
stderr='/dev/null'):
'''
argv: list of executable and arguments. First item is the executable. e.g.
['/bin/sleep', '100']
pidfile: filename. pid of the daemon child process will be written to the
file. This is useful for monitoring services that need a pid to stop the
daemon, etc.
Calls daemonize, which exits the calling process and continues in the child
process. Therefore all code after calling daemonize_command will be
executed in the daemon process.
'''
# use absolute path, since daemonize() changes cwd
pidfile = os.path.abspath(pidfile)
daemonize(stdin, stdout, stderr)
# now we are in the daemon process
# save pid to a file
if pidfile:
setpid(os.getpid(), pidfile)
# do not spawn a subprocess, since the daemon process is the one we want to
# start/stop/restart/etc.
os.execvp(argv[0], argv)
def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
'''
stdin, stdout, stderr: filename that will be opened and used to replace the
standard file descriptors is sys.stdin, sys.stdout, sys.stderr. Default to
/dev/null. Note that stderr is opened unbuffered, so if it shares a file
with stdout then interleaved output may not appear in the order that you
expect.
Turn current process into a daemon.
returns: nothing in particular.
'''
# use absolute path, since daemonize() changes cwd
stdin = os.path.abspath(stdin)
stdout = os.path.abspath(stdout)
stderr = os.path.abspath(stderr)
# Do first fork
try:
pid = os.fork()
except OSError as e:
sys.stderr.write("fork #1 failed: (%d) %s\n"%(e.errno, e.strerror))
sys.exit(1)
if pid > 0:
sys.exit(0) # exit parent
# Decouple from parent environment.
os.chdir("/")
os.umask(0)
os.setsid()
# Do second fork.
try:
pid = os.fork()
except OSError as e:
sys.stderr.write("fork #2 failed: (%d) %s\n"%(e.errno, e.strerror))
sys.exit(1)
if pid > 0:
sys.exit(0) # exit parent
# Now I am a daemon!
# Redirect standard file descriptors. First open the new files, perform
# hack if necessary, flush any existing output, and dup new files to std
# streams.
si = open(stdin, 'r')
so = open(stdout, 'a+')
se = open(stderr, 'a+', 0)
# hack and bug: when sys.stdin.close() has already been called,
# os.dup2 throws an exception: ValueError: I/O operation on closed file
# This hack attempts to detect whether any of the std streams
# have been closed and if so opens them to a dummy value which
# will get closed by os.dup2, which I like better than
# an exception being thrown.
if sys.stdin.closed: sys.stdin = open('/dev/null', 'r')
if sys.stdout.closed: sys.stdout = open('/dev/null', 'a+')
if sys.stderr.closed: sys.stderr = open('/dev/null', 'a+')
sys.stdout.flush()
sys.stderr.flush()
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def getpid(pidfile):
'''
read pid from pidfile
return: pid
'''
pid = None
if os.path.isfile(pidfile):
with open(pidfile) as fh:
pid = int(fh.read().strip())
return pid
def setpid(pid, pidfile):
'''
save pid to pidfile
'''
with open(pidfile, 'w') as fh:
fh.write('{}\n'.format(pid))
def running(pid):
"""
pid: a process id
Return: False if the pid is None or if the pid does not match a
currently-running process.
Derived from code in http://pypi.python.org/pypi/python-daemon/ runner.py
"""
if pid is None:
return False
try:
os.kill(pid, signal.SIG_DFL)
except OSError, exc:
if exc.errno == errno.ESRCH:
# The specified PID does not exist
return False
return True
def main():
# daemoncmd.py <command>
# daemoncmd.py start --pidfile <file> [--stdin <file>] [--stdout <file>] \
# [--stderr <file>] <command>
# daemoncmd.py restart --pidfile <file> [--stdin <file>] [--stdout <file>]\
# [--stderr <file>] <command>
# daemoncmd.py stop --pidfile <file>
# daemoncmd.py status --pidfile <file>
parser = argparse.ArgumentParser(
description=('Start, stop, restart or get the status of a daemon '
'that runs a command.'))
subparsers = parser.add_subparsers(dest='action')
# create the parser for the "start" command
startParser = subparsers.add_parser('start',
help='start a daemon to run a command')
startParser.add_argument(
'--pidfile', required=True,
help='file in which to store the pid of the started daemon process')
startParser.add_argument('--stdin', default='/dev/null',
help='redirect daemon stdin from this file')
startParser.add_argument('--stdout', default='/dev/null',
help='redirect daemon stdout to this file')
startParser.add_argument('--stderr', default='/dev/null',
help='redirect daemon stderr to this file')
startParser.add_argument(
'cmd',
help=('the executable/command that the daemon will run. i.e. a '
'server that listens on a port for incoming connections.'))
startParser.add_argument('args', nargs='*',
help='options or arguments to the command')
stopParser = subparsers.add_parser('stop', help='stop a running daemon')
stopParser.add_argument('--pidfile', required=True,
help='file containing the pid of daemon process')
stopParser = subparsers.add_parser(
'status', help='print the status of a daemon process')
stopParser.add_argument('--pidfile', required=True,
help='file containing the pid of daemon process')
restartParser = subparsers.add_parser(
'restart', help='restart a daemon to run a command')
restartParser.add_argument(
'--pidfile', required=True,
help='file in which to store the pid of the started daemon process')
restartParser.add_argument('--stdin', default='/dev/null',
help='redirect daemon stdin from this file')
restartParser.add_argument('--stdout', default='/dev/null',
help='redirect daemon stdout to this file')
restartParser.add_argument('--stderr', default='/dev/null',
help='redirect daemon stderr to this file')
restartParser.add_argument(
'cmd',
help=('the executable/command that the daemon will run. i.e. a '
'server that listens on a port for incoming connections.'))
restartParser.add_argument('args', nargs='*',
help='options or arguments to the command')
args = parser.parse_args()
if args.action == START:
start([args.cmd] + args.args, args.pidfile, args.stdin, args.stdout, args.stderr)
elif args.action == RESTART:
restart([args.cmd] + args.args, args.pidfile, args.stdin, args.stdout, args.stderr)
elif args.action == STOP:
stop(args.pidfile)
elif args.action == STATUS:
status(args.pidfile)
if __name__ == '__main__':
main()