tests/killdaemons.py
branchstable
changeset 1127 79d7aa832cf4
parent 1126 dbcb11553a3b
child 1128 7f09e7ac63a7
equal deleted inserted replaced
1126:dbcb11553a3b 1127:79d7aa832cf4
     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
    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