20 lines
718 B
Python
20 lines
718 B
Python
# python3 run.py <seconds> <log> <cmd...>: runs cmd in its own session, kills
|
|
# the whole group on timeout and sweeps it after a normal exit. Exit 124 on
|
|
# timeout, else the command's code.
|
|
import os, signal, subprocess, sys
|
|
secs, log, cmd = int(sys.argv[1]), sys.argv[2], sys.argv[3:]
|
|
with open(log, "w") as out:
|
|
p = subprocess.Popen(cmd, stdout=out, stderr=subprocess.STDOUT, start_new_session=True)
|
|
try:
|
|
rc = p.wait(timeout=secs)
|
|
except subprocess.TimeoutExpired:
|
|
os.killpg(p.pid, signal.SIGKILL)
|
|
p.wait()
|
|
out.write(f"\nTIMEOUT after {secs} s\n")
|
|
rc = 124
|
|
try:
|
|
os.killpg(p.pid, signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
sys.exit(rc)
|