查询僵尸进程并杀死僵尸进程

所谓僵尸进程,就父进程已经结束了,子进程还在运行中,
并且windows 没有僵尸进程一说
命令:

ps -A -o stat,ppid,pid,cmd | grep -e '^[zZ]'
代码实现:
# -*- coding: utf-8 -*-
import time
import psutil
# 查看僵尸进程
def restart_process():
    pids = psutil.pids()
    for pid in pids:
        try:
            process = psutil.Process(pid=pid)
            command = " ".join(process.cmdline())
            command = command.lower()
            if "chromedriver.exe" in command or "chrome.exe" in command:
                process.kill()
                process.send_signal(9)
                print(pid)
        except:
            pass
        time.sleep(1.5)

if __name__ == '__main__':
    restart_process()

你可能感兴趣的:(python)