Python实现进程监控 避免程序意外终止

2022-05-20 10:40
import os
import psutil
import time

# 被检测程序路径
name = r""

# 进程检测函数
def checkprocess(processname):
    pl = psutil.pids()
    for pid in pl:
        if psutil.Process(pid).name() == processname:
           return pid


#监控函数
def run():
    if isinstance(checkprocess('进程名'),int):
        print("进程存在")
    else:
        print("进程不存在")
        code(name)
    time.sleep(3)
    run()

run()

 

posted @ 2022-06-02 14:55  Haibara-Z3r0  阅读(411)  评论(0编辑  收藏  举报