tests/killdaemons.py
branchstable
changeset 1126 dbcb11553a3b
parent 1125 20a680e7f09f
child 1127 79d7aa832cf4
--- a/tests/killdaemons.py	Wed Apr 14 17:58:10 2010 +0900
+++ b/tests/killdaemons.py	Sun Aug 19 16:41:09 2012 +0200
@@ -2,24 +2,39 @@
 
 import os, time, errno, signal
 
-# Kill off any leftover daemon processes
-try:
-    fp = open(os.environ['DAEMON_PIDS'])
-    for line in fp:
-        try:
-            pid = int(line)
-        except ValueError:
-            continue
-        try:
-            os.kill(pid, 0)
-            os.kill(pid, signal.SIGTERM)
-            for i in range(10):
-                time.sleep(0.05)
+def killdaemons(pidfile, tryhard=True, remove=False, logfn=None):
+    if not logfn:
+        logfn = lambda s: s
+    # Kill off any leftover daemon processes
+    try:
+        fp = open(pidfile)
+        for line in fp:
+            try:
+                pid = int(line)
+            except ValueError:
+                continue
+            try:
                 os.kill(pid, 0)
-            os.kill(pid, signal.SIGKILL)
-        except OSError, err:
-            if err.errno != errno.ESRCH:
-                raise
-    fp.close()
-except IOError:
-    pass
+                logfn('# Killing daemon process %d' % pid)
+                os.kill(pid, signal.SIGTERM)
+                if tryhard:
+                    for i in range(10):
+                        time.sleep(0.05)
+                        os.kill(pid, 0)
+                else:
+                    time.sleep(0.1)
+                    os.kill(pid, 0)
+                logfn('# Daemon process %d is stuck - really killing it' % pid)
+                os.kill(pid, signal.SIGKILL)
+            except OSError, err:
+                if err.errno != errno.ESRCH:
+                    raise
+        fp.close()
+        if remove:
+            os.unlink(pidfile)
+    except IOError:
+        pass
+
+if __name__ == '__main__':
+    killdaemons(os.environ['DAEMON_PIDS'])
+