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 |
|
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 |
4 |
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 |