File size: 660 Bytes
11e4a86 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import subprocess
import os
import signal
def kill_port_linux(port):
try:
# Find processes using the port
result = subprocess.check_output(f'lsof -i :{port}', shell=True).decode()
lines = result.strip().split('\n')
for line in lines[1:]: # skip header line
parts = line.split()
pid = int(parts[1])
os.kill(pid, signal.SIGKILL)
print(f'Killed PID {pid} using port {port}')
except subprocess.CalledProcessError:
print(f'No process is using port {port}.')
except Exception as e:
print(f'Error: {e}')
if __name__ == '__main__':
kill_port_linux(5000) |