python 杀死占用端口号的进程
import os
def kill_process(*pids):
for pid in pids:
a = os.kill(pid, signal.SIGKILL)
print('已杀死pid为%s的进程, 返回值是:%s' % (pid, a))
def get_pid(*ports):
#其中\"为转义"
pids = []
for port in ports:
pid = os.popen("netstat -nlp | grep :%s | awk '{print $7}' | awk -F\" / \" '{ print $1 }'" % (port)).read().split('/')[0]
if pid:
pids.append(int(pid))
return pids
# 杀死占用端口号的ps进程
ps_ports = ["23846", "23847", "23848", "23849", "23850", "23851"]
kill_process(*get_pid(*ps_ports))
还没有评论,来说两句吧...