python 杀死占用端口号的进程

电玩女神 2022-12-25 05:51 418阅读 0赞
  1. import os
  2. def kill_process(*pids):
  3. for pid in pids:
  4. a = os.kill(pid, signal.SIGKILL)
  5. print('已杀死pid为%s的进程, 返回值是:%s' % (pid, a))
  6. def get_pid(*ports):
  7. #其中\"为转义"
  8. pids = []
  9. for port in ports:
  10. pid = os.popen("netstat -nlp | grep :%s | awk '{print $7}' | awk -F\" / \" '{ print $1 }'" % (port)).read().split('/')[0]
  11. if pid:
  12. pids.append(int(pid))
  13. return pids
  14. # 杀死占用端口号的ps进程
  15. ps_ports = ["23846", "23847", "23848", "23849", "23850", "23851"]
  16. kill_process(*get_pid(*ps_ports))

发表评论

表情:
评论列表 (有 0 条评论,418人围观)

还没有评论,来说两句吧...

相关阅读