import re
import subprocess, os
SERVER_PORT = 8001
get_process_prot_cmd = f'ss -anpl | grep -w {SERVER_PORT}'
res = subprocess.run(
get_process_prot_cmd,
shell=True,
stdout=subprocess.PIPE,
)
if res.returncode == 0:
parse_res = res.stdout.decode()
pids = re.findall(r'pid=(\d+)', parse_res)
for pid in pids:
os.system(f'kill -9 {pid}')
python杀掉监听某端口的进程
于 2023-01-04 18:15:55 首次发布