Show More
@@ -1,40 +1,53 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 | import os, time, errno, signal |
|
3 | import os, time, errno, signal | |
4 |
|
4 | |||
|
5 | if os.name =='nt': | |||
|
6 | import ctypes | |||
|
7 | def kill(pid, logfn, tryhard=True): | |||
|
8 | logfn('# Killing daemon process %d' % pid) | |||
|
9 | PROCESS_TERMINATE = 1 | |||
|
10 | handle = ctypes.windll.kernel32.OpenProcess( | |||
|
11 | PROCESS_TERMINATE, False, pid) | |||
|
12 | ctypes.windll.kernel32.TerminateProcess(handle, -1) | |||
|
13 | ctypes.windll.kernel32.CloseHandle(handle) | |||
|
14 | else: | |||
|
15 | def kill(pid, logfn, tryhard=True): | |||
|
16 | try: | |||
|
17 | os.kill(pid, 0) | |||
|
18 | logfn('# Killing daemon process %d' % pid) | |||
|
19 | os.kill(pid, signal.SIGTERM) | |||
|
20 | if tryhard: | |||
|
21 | for i in range(10): | |||
|
22 | time.sleep(0.05) | |||
|
23 | os.kill(pid, 0) | |||
|
24 | else: | |||
|
25 | time.sleep(0.1) | |||
|
26 | os.kill(pid, 0) | |||
|
27 | logfn('# Daemon process %d is stuck - really killing it' % pid) | |||
|
28 | os.kill(pid, signal.SIGKILL) | |||
|
29 | except OSError, err: | |||
|
30 | if err.errno != errno.ESRCH: | |||
|
31 | raise | |||
|
32 | ||||
5 | def killdaemons(pidfile, tryhard=True, remove=False, logfn=None): |
|
33 | def killdaemons(pidfile, tryhard=True, remove=False, logfn=None): | |
6 | if not logfn: |
|
34 | if not logfn: | |
7 | logfn = lambda s: s |
|
35 | logfn = lambda s: s | |
8 | # Kill off any leftover daemon processes |
|
36 | # Kill off any leftover daemon processes | |
9 | try: |
|
37 | try: | |
10 | fp = open(pidfile) |
|
38 | fp = open(pidfile) | |
11 | for line in fp: |
|
39 | for line in fp: | |
12 | try: |
|
40 | try: | |
13 | pid = int(line) |
|
41 | pid = int(line) | |
14 | except ValueError: |
|
42 | except ValueError: | |
15 | continue |
|
43 | continue | |
16 | try: |
|
44 | kill(pid, logfn, tryhard) | |
17 | os.kill(pid, 0) |
|
|||
18 | logfn('# Killing daemon process %d' % pid) |
|
|||
19 | os.kill(pid, signal.SIGTERM) |
|
|||
20 | if tryhard: |
|
|||
21 | for i in range(10): |
|
|||
22 | time.sleep(0.05) |
|
|||
23 | os.kill(pid, 0) |
|
|||
24 | else: |
|
|||
25 | time.sleep(0.1) |
|
|||
26 | os.kill(pid, 0) |
|
|||
27 | logfn('# Daemon process %d is stuck - really killing it' % pid) |
|
|||
28 | os.kill(pid, signal.SIGKILL) |
|
|||
29 | except OSError, err: |
|
|||
30 | if err.errno != errno.ESRCH: |
|
|||
31 | raise |
|
|||
32 | fp.close() |
|
45 | fp.close() | |
33 | if remove: |
|
46 | if remove: | |
34 | os.unlink(pidfile) |
|
47 | os.unlink(pidfile) | |
35 | except IOError: |
|
48 | except IOError: | |
36 | pass |
|
49 | pass | |
37 |
|
50 | |||
38 | if __name__ == '__main__': |
|
51 | if __name__ == '__main__': | |
39 | killdaemons(os.environ['DAEMON_PIDS']) |
|
52 | killdaemons(os.environ['DAEMON_PIDS']) | |
40 |
|
53 |
General Comments 0
You need to be logged in to leave comments.
Login now