7 stdin_ = os.dup(sys.stdin.fileno())
8 stdout_ = os.dup(sys.stdout.fileno())
16 if signum != 14:
return
17 raise Timeout(
"Timeout reached.")
19 signal.signal(signal.SIGALRM, alarm_handler)
22 sys.stderr.write(
"watchdog: " + s +
"\n");
32 env[
"WATCHDOG_FD"] = str(wd)
34 p = subprocess.Popen(args.pargs, preexec_fn=preexec, stdin=stdin_, stdout=stdout_)
40 ch = os.read(fd, 1024)
45 return False, p.wait()
47 log(
"Received: %s, timer reset." % repr(ch))
52 log(
"Timeout reached, taking action.")
55 p.send_signal(args.action)
67 log(
"Program exitted, killed: %s, code: %d." % (killed, ret, ))
69 if killed
and args.restart:
77 parser.add_argument(
"-t", type=int, default=
"2", help=
"Timeout in seconds.")
78 parser.add_argument(
"-s", type=int, default=
"2000", help=
"Signal to send.")
79 parser.add_argument(
"-r",
"--restart", action=
"store_true", default=
False, help=
"Restart the process after killing it.")
80 parser.add_argument(
"pargs", nargs=argparse.REMAINDER)
82 group = parser.add_mutually_exclusive_group()
83 group.add_argument(
'--term', action=
'store_const', dest=
"action", const=signal.SIGTERM, default=signal.SIGTERM)
84 group.add_argument(
'--kill', action=
'store_const', dest=
"action", const=signal.SIGKILL)
86 if __name__ ==
"__main__":
87 args = parser.parse_args()