Kill process after timeout (python script) 2009-01-11 =====================================================
This very simple and small python script starts a process, and kills it if it has not ended in a timely fashion.
Very simple, and uses polling instead of the alarm() function, but enough for me at the moment.
#!/bin/python from sys import argv from sys import exit from time import sleep from time import time from os import kill from signal import SIGKILL import subprocess def print_help(): print "Usage: $0 timeout command [args ...]" if len(argv) < 3: print_help()exit(1) proc = subprocess.Popen(argv[2:]) start = time() while time() - start < int(argv): sleep(0.2) ret = proc.poll() if ret != None: exit(ret) # time's up: kill the proc: print "Killing PID %d, it did not end after %d seconds." % (proc.pid,int(argv)) kill(proc.pid, SIGKILL) exit(proc.wait())