diff --git a/cluster/cluster.py b/cluster/cluster.py index 22205b8caedf12484339c0a06b540a5d0570b0f3..484e259f3a86adede711c4bc59df68bc4068f212 100644 --- a/cluster/cluster.py +++ b/cluster/cluster.py @@ -252,17 +252,26 @@ def kill_process(hostname: str, pid: int, signal="TERM"): """ Kill a process with the given `pid` on the specified `hostname` :param hostname: Hostname where the process is located. - :param pid: PID of the process to kill. + :param pid: PGID of the process to kill. :param signal: Signal used to kill the process. One of "TERM", "KILL" or "INT". """ + import signal as pysignal + assert signal in ("TERM", "KILL", "INT") logging.debug(f"Killing PGID {pid} on {hostname}") - args = ["kill", f"-{signal}", "--", f"-{pid}"] if not is_local(hostname): - args = ["ssh", hostname, "--"] + args - res = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - if res.returncode != 0: - logging.error( - f"error: {res.returncode} {res.stdout.decode().strip()} {res.stderr.decode().strip()}") - return False + args = ["ssh", hostname, "--", "kill", f"-{signal}", f"-{pid}"] + res = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + if res.returncode != 0: + logging.error( + f"error: {res.returncode} {res.stdout.decode().strip()} {res.stderr.decode().strip()}") + return False + else: + if signal == "TERM": + signal = pysignal.SIGTERM + elif signal == "KILL": + signal = pysignal.SIGKILL + elif signal == "INT": + signal = pysignal.SIGINT + os.killpg(pid, signal) return True