import subprocess
import threading
def kill_process(port, pid):
try:
subprocess.run(['kill', '-9', pid], check=False)
print(f"Process using port {port} with PID {pid} killed.")
except subprocess.CalledProcessError as e:
print(f"Error killing process using port {port} with PID {pid}: {e}")
def find_and_kill(port):
try:
command = f'lsof -ti:{port}'
process_ids = subprocess.check_output(command, shell=True, text=False).split()
for pid in process_ids:
threading.Thread(target=kill_process, args=(port, pid)).start()
except subprocess.CalledProcessError:
print(f"No process found using port {port}.")
# Main loop
ports_to_kill = [5003, 5004, 5005, 5253]
for port in ports_to_kill:
threading.Thread(target=find_and_kill, args=(port,)).start()