【Python】python找到端口占用,并杀死进程,linux

参考:python找到端口占用,并杀死进程,windows

# used for linux
import os,sys
def free_port(port):
    with os.popen('lsof -i:'+str(port)) as res:
        res = res.read().split('\n')
    result = []
    for line in res:
        temp = [i for i in line.split(' ') if i != '']
        if len(temp) > 4:
            result.append({'pid': temp[1], 'comand': temp[0], 'user': temp[2],'nmae':temp[8]})
    # print("占用{}端口的进程如下:".format(port))
    # print(result)
    if len(result)>1:
        for i in range(1,len(result)): # 每个line都是一个字典
            pid=result[i]['pid']
            result = os.popen("kill -9 "+str(pid))
            print("杀死占用{}的进程号{},成功".format(port,pid))

你可能感兴趣的:(教程,python,linux,运维,服务器)