baoxiang/backend/force_kill_port.py

23 lines
731 B
Python
Raw Normal View History

2025-12-16 18:06:50 +08:00
"""
Force kill all Python processes on port 8000
"""
import psutil
import os
import signal
def kill_process_on_port(port):
"""Kill process listening on specified port"""
for conn in psutil.net_connections():
if conn.laddr.port == port and conn.status == 'LISTEN':
try:
proc = psutil.Process(conn.pid)
# Kill the process and all its children
proc.kill()
print(f"Killed process {conn.pid} on port {port}")
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
print(f"Could not kill process {conn.pid}: {e}")
# Kill all processes on port 8000
kill_process_on_port(8000)
print("Port 8000 should now be free")