Files
Live-streaming/scripts/_sampler_ctl.py

34 lines
921 B
Python

import os
import sys
import time
import psutil
TARGET = "perf_sampler.py"
mode = sys.argv[1] if len(sys.argv) > 1 else "kill"
me = os.getpid()
matched = []
for p in psutil.process_iter(["name", "cmdline"]):
if p.pid == me:
continue
try:
parts = [x for x in (p.info["cmdline"] or [])]
# only match when the script is the program argument, not substring of -c code
if any(part.endswith(TARGET) for part in parts):
matched.append(p)
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
if mode == "kill":
for p in matched:
try:
p.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
time.sleep(1)
print("killed:", [p.pid for p in matched])
else:
import datetime
for p in matched:
print(p.pid, p.name(), datetime.datetime.fromtimestamp(p.create_time()).strftime("%H:%M:%S"))