美文网首页
python 杀进程

python 杀进程

作者: hehehehe | 来源:发表于2021-05-27 20:41 被阅读0次
    
    import logging
    import os, signal
    
    logger = logging.getLogger("")
    
    program = 'de_dump.py'
    
    out = os.popen(f"ps aux | grep {program}").read()
    for line in out.splitlines():
        print(line)
        if program in line:
            pid = int(line.split()[1])
            try:
                os.kill(pid, signal.SIGKILL)
                print(f'已杀死pid为{pid}的进程')
            except OSError:
                print('没有如此进程!!!')
    
    
    import psutil
    
    def kill_process_by_name(process_name):
        pid_list = psutil.pids()
        for pid in pid_list:
            try:
                p = psutil.Process(pid)
                cmd = p.cmdline()
                if process_name in cmd: 
                    print(cmd)
                    p.terminate()           
            except Exception:
                pass
    
    
    if __name__ == '__main__':
        kill_process_by_name('b.py')
    

    相关文章

      网友评论

          本文标题:python 杀进程

          本文链接:https://www.haomeiwen.com/subject/qmidsltx.html