import os, re, signal, subprocess, time

PAT = re.compile(r"shoot_09[2]5|agent-browser|chrome-linux|headless_shell")
me = os.getpid()
killed = 0
out = subprocess.run(["ps", "-eo", "pid=,args="], capture_output=True, text=True).stdout
for line in out.splitlines():
    line = line.strip()
    if not line:
        continue
    sp = line.split(None, 1)
    if len(sp) < 2:
        continue
    pid, args = int(sp[0]), sp[1]
    if pid == me or "kill_procs" in args:
        continue
    if PAT.search(args):
        try:
            os.kill(pid, signal.SIGKILL)
            killed += 1
        except Exception:
            pass
time.sleep(3)
out = subprocess.run(["ps", "-eo", "args="], capture_output=True, text=True).stdout
left = sum(1 for l in out.splitlines() if PAT.search(l))
print("killed=%d remaining=%d" % (killed, left))
