File size: 622 Bytes
bad179a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env python3
"""Review stand-in for taskkill.exe: "taskkill /PID <n> /T /F" ends the process <n> and everything below it (like /T on Windows)."""
import os, signal, sys
args = sys.argv[1:]
pid = int(args[args.index('/PID') + 1])
kids = {}
for d in os.listdir('/proc'):
if d.isdigit():
try: kids.setdefault(int(open(f'/proc/{d}/stat').read().rsplit(')', 1)[1].split()[1]), []).append(int(d))
except OSError: pass
def tree(p): return [p] + [x for k in kids.get(p, []) for x in tree(k)]
for p in tree(pid) if '/T' in args else [pid]:
try: os.kill(p, signal.SIGKILL)
except OSError: pass
|