timeout.py 1.22 KB
Newer Older
Simon Marlow's avatar
Simon Marlow committed
1
#!/usr/bin/env python
2

3
try:
4

5 6 7 8 9
    import errno
    import os
    import signal
    import sys
    import time
10

11 12
    secs = int(sys.argv[1])
    cmd = sys.argv[2]
13

14 15 16 17 18 19 20 21 22 23
    def killProcess(pid):
        os.killpg(pid, signal.SIGKILL)
        for x in range(10):
            try:
                time.sleep(0.3)
                r = os.waitpid(pid, os.WNOHANG)
                if r == (0, 0):
                    os.killpg(pid, signal.SIGKILL)
                else:
                    return
24
            except OSError as e:
25 26 27 28 29 30 31 32 33 34
                if e.errno == errno.ECHILD:
                    return
                else:
                    raise e

    pid = os.fork()
    if pid == 0:
        # child
        os.setpgrp()
        os.execvp('/bin/sh', ['/bin/sh', '-c', cmd])
35
    else:
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        # parent
        def handler(signum, frame):
            sys.stderr.write('Timeout happened...killing process...\n')
            killProcess(pid)
            sys.exit(99)
        old = signal.signal(signal.SIGALRM, handler)
        signal.alarm(secs)
        (pid2, res) = os.waitpid(pid, 0)
        if (os.WIFEXITED(res)):
            sys.exit(os.WEXITSTATUS(res))
        else:
            sys.exit(res)

except KeyboardInterrupt:
    sys.exit(98)
except:
    raise
53