首页 > 系统相关 >python 根据端口号杀死kill 在linux部署的进程

python 根据端口号杀死kill 在linux部署的进程

时间:2022-11-24 16:57:56浏览次数:41  
标签:python pid --- kill print data port 端口号

import subprocess
import os
def getPid(port):
    """获取进程pid"""
    try:
        back = subprocess.Popen("""lsof -i:%s | awk 'NR==2{print $2}'""" % (port), shell=True, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE).communicate()
        data = back[0].decode().split('\n')
    except Exception as e:
        print('error:', e)
        data = []
    print('端口对应的pid:', data)
    return data


def killPid(pid):
    """杀死进程"""
    cmd = "kill -s 9 {}".format(pid)
    print(cmd)
    result = os.system(cmd)
    if result == 0:
        print("kill {} success".format(pid))
    else:
        print("kill {} error".format(pid))
    print('- -- -- --- ---- --- --- ---- --- --- -- --')
    print('end')


def run(port):
    data = getPid(port)
    pid = data[0]
    print('pid:', pid)
    print('pid:', len(pid))
    if len(pid) > 0:
        killPid(pid)
    else:
        print('未查到端口-{}进程:'.format(port))


run(8001)

 

标签:python,pid,---,kill,print,data,port,端口号
From: https://www.cnblogs.com/jun-zhou/p/16922407.html

相关文章

  • python sqlserver
    python连接并简单操作SQLserver数据库实验环境:python版本3.9Python3.9.7(tags/v3.9.7:1016ef3,Aug302021,20:19:38)[MSCv.192964bit(AMD64)]onwin32Type"he......
  • python8
    一、创建计算BMI指数的模块deffun_bmi(person,height,weight):'''功能:根据身高体重计算BMI指数person:姓名height:身高,单位:米weight:体重,单......
  • python arp欺骗
     ls(ARP())hwtype    :XShortField                        =1              ('1')ptype     :XShortEnumField......
  • centos下python2.7.5升级到python3.5版本
    1.我们先下载python3.5的版本在我们的服务器任意一个文件夹,博主是放在home目录下,我们先进入到该目录:百度网盘python3.5.2下载链接:链接:https://pan.baidu.com/s/1Wp04mcKo......
  • python练习题-函数-练习(一)
    1.编写一个函数cacluate,可以接收任意个数,返回的是一个元组,其中元组的第一个值为所有参数的平均值,第二个值是大于平均值的所有数。defcacluate(*args):printargs,......
  • Chapter 2: Python Language Basics, IPython, and Jupyter Notebooks 个人理解与问题
    2.2IPython基础2.2.2运行Jupyternotebook  在终端输入\(Jupyter\quadnotebook\),会在默认浏览器打开\(Jupyter\),但是注意路径问题,如果我们在D:\Python_Code\IPy......
  • python装饰器 - 修改函数返回值
    deff(n):n+=1print("hello:{}".format(n))returnn+1ret=f(9)print("Functionreturnvalue:",ret)在没有装饰器的情况下,运行结果如下hello:1......
  • 【python自动化】02. pywin32库自动操作键鼠(保姆级代码注释)
     目录源码和工具下载大漠综合工具->坐标和窗口信息抓取在你的桌面上新建一个记事本用于后面的代码测试完整项目源码实现思路介绍win32的基础思路基本步......
  • Python第十周
    一.   实验目的和要求二.  实验环境 python3.1064-bit三.  实验过程实例1代码如下:1deffun_bmi(person,height,weight):2'''功能:根据身高和体......
  • 【Python字符串方法】字符串类型判断、大小写转化、拆分和组合、填充和对齐
    去空白字符str.strip():去掉字符串左右两边的空格str.lstrip():去掉字符串左边的空格str.rstrip():去掉字符串右边的空格以上函数返回从处理后的字符串,并不在原对象上......