zoukankan      html  css  js  c++  java
  • python3 进程中 获取进程号和杀死进程

    转载:https://blog.csdn.net/Teddycxr/article/details/80804383

    遍历搜寻及测试总结三种方法

    1. signal

    import os,signal
    
    out=os.popen("ps aux | grep xx.py").read()
    
    for line in out.splitlines():
        print(line)
        if 'BcexServices.py' in line:
            pid = int(line.split()[1])
            print(pid)
        os.kill(pid,signal.SIGKILL)
    
    def kill(pid):
        try:
            a = os.kill(pid, signal.SIGKILL)
            print('已杀死pid为%s的进程, 返回值是:%s' % (pid, a))
    
        except OSError:
            print('没有如此进程!!!')

    2. psutil

    import psutil
    
    def processinfo(processName):
        pids = psutil.pids()
        for pid in pids:
            # print(pid)
            p = psutil.Process(pid)
            # print(p.name)
            if p.name() == processName:
                print(pid)
                return True  # 如果找到该进程则打印它的PID,返回true
        return False  # 没有找到该进程,返回false
    
    processinfo('你的文件名.py')

    3. psutil 的另外一种方式

    for proc in psutil.process_iter():
        print("pid-%d,name:%s" % (proc.pid,proc.name()))
  • 相关阅读:
    delphi try except语句 和 try finally语句用法以及区别
    正向代理与反向代理(转)
    kbmMW功能
    problem 202,263、232、21、231
    leetcode day8
    leetcode day7
    VS2013 opencv2.4.8
    web 前端routine
    leetcode day6
    leetcode day5
  • 原文地址:https://www.cnblogs.com/bruce-he/p/14721359.html
Copyright © 2011-2022 走看看