import datetime import sys import psutil targets = [] for p in psutil.process_iter(["name", "cmdline"]): try: cmd = " ".join(p.info["cmdline"] or []) except (psutil.NoSuchProcess, psutil.AccessDenied): continue if "main.py" in cmd and "--role" in cmd: targets.append(p) for p in targets: try: parent = p.parent() print( p.pid, p.name(), "started", datetime.datetime.fromtimestamp(p.create_time()).strftime("%m-%d %H:%M:%S"), "parent", parent.pid if parent else None, parent.name() if parent else "", ) except psutil.NoSuchProcess: print(p.pid, "exited") if not targets: print("NO_SERVICE_PROCESS")