ssh to a remote machine and kill the specified processes.
(ip, port, pids)
| 36 | |
| 37 | |
| 38 | def kill_process(ip, port, pids): |
| 39 | """ssh to a remote machine and kill the specified processes.""" |
| 40 | curr_pid = os.getpid() |
| 41 | killed_pids = [] |
| 42 | # If we kill child processes first, the parent process may create more again. This happens |
| 43 | # to Python's process pool. After sorting, we always kill parent processes first. |
| 44 | pids.sort() |
| 45 | for pid in pids: |
| 46 | assert curr_pid != pid |
| 47 | print("kill process {} on {}:{}".format(pid, ip, port), flush=True) |
| 48 | kill_cmd = ( |
| 49 | "ssh -o StrictHostKeyChecking=no -p " |
| 50 | + str(port) |
| 51 | + " " |
| 52 | + ip |
| 53 | + " 'kill {}'".format(pid) |
| 54 | ) |
| 55 | subprocess.run(kill_cmd, shell=True) |
| 56 | killed_pids.append(pid) |
| 57 | # It's possible that some of the processes are not killed. Let's try again. |
| 58 | for i in range(3): |
| 59 | killed_pids = get_killed_pids(ip, port, killed_pids) |
| 60 | if len(killed_pids) == 0: |
| 61 | break |
| 62 | else: |
| 63 | killed_pids.sort() |
| 64 | for pid in killed_pids: |
| 65 | print( |
| 66 | "kill process {} on {}:{}".format(pid, ip, port), flush=True |
| 67 | ) |
| 68 | kill_cmd = ( |
| 69 | "ssh -o StrictHostKeyChecking=no -p " |
| 70 | + str(port) |
| 71 | + " " |
| 72 | + ip |
| 73 | + " 'kill -9 {}'".format(pid) |
| 74 | ) |
| 75 | subprocess.run(kill_cmd, shell=True) |
| 76 | |
| 77 | |
| 78 | def get_killed_pids(ip, port, killed_pids): |
no test coverage detected