kill process with python

You can retrieve the process id (PID) given it name using pgrep command like this: import subprocess import signal import os from datetime import datetime as dt process_name = sys.argv[1] log_file_name = sys.argv[2] proc = subprocess.Popen([“pgrep”, process_name], stdout=subprocess.PIPE) # Kill process. for pid in proc.stdout: os.kill(int(pid), signal.SIGTERM) # Check if the process that we killed … Read more

Kill or terminate subprocess when timeout?

You could do something like this: import subprocess as sub import threading class RunCmd(threading.Thread): def __init__(self, cmd, timeout): threading.Thread.__init__(self) self.cmd = cmd self.timeout = timeout def run(self): self.p = sub.Popen(self.cmd) self.p.wait() def Run(self): self.start() self.join(self.timeout) if self.is_alive(): self.p.terminate() #use self.p.kill() if process needs a kill -9 self.join() RunCmd([“./someProg”, “arg1”], 60).Run() The idea is that you … Read more