From 69ec2ce7a949453b8cd7195bcfeb3eaec7825623 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Wed, 28 May 2025 14:29:20 +0300 Subject: [PATCH] kill stales --- kill_stale_processes.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 kill_stale_processes.py diff --git a/kill_stale_processes.py b/kill_stale_processes.py new file mode 100644 index 0000000..7d7df6c --- /dev/null +++ b/kill_stale_processes.py @@ -0,0 +1,40 @@ +import psutil +import sys + +try: + current_pid = psutil.Process().pid + processes = [ + p for p in psutil.process_iter() + if any(x in p.name().lower() for x in ["python", "tensorboard"]) + and any(x in ' '.join(p.cmdline()) for x in ["scalping", "training", "tensorboard"]) + and p.pid != current_pid + ] + for p in processes: + try: + p.kill() + print(f"Killed process: PID={p.pid}, Name={p.name()}") + except Exception as e: + print(f"Error killing PID={p.pid}: {e}") + + killed_pids = set() + for port in range(8050, 8052): + for proc in psutil.process_iter(): + if proc.pid == current_pid: + continue + try: + for conn in proc.connections(kind="inet"): + if conn.laddr.port == port: + if proc.pid not in killed_pids: + proc.kill() + print(f"Killed process on port {port}: PID={proc.pid}, Name={proc.name()}") + killed_pids.add(proc.pid) + except (psutil.AccessDenied, psutil.NoSuchProcess): + continue + except Exception as e: + print(f"Error checking/killing PID={proc.pid} for port {port}: {e}") + if not any(pid for pid in killed_pids): + print(f"No process found using port {port}") + print("Stale processes killed") +except Exception as e: + print(f"Error in kill_stale_processes.py: {e}") + sys.exit(1) \ No newline at end of file