40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
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) |