Python定时检测无响应进程并重启(Win/Linux)

可选的工具包:

  • schedule:定时任务调度库,如果多线程执行注意不要设置成死循环
  • psutil:跨平台库, 能够轻松实现获取系统运行的进程和系统利用率

schedule

import schedule
import time
from datetime import datetime
from schedule import every, repeat

def job(name):
    print("name is : ", name)

name = "scheduler"
schedule.every(10).minutes.do(job, name)
schedule.every().hour.do(job, name)
schedule.every().day.at("10:30").do(job, name)
schedule.every(5).to(10).days.do(job, name)
schedule.every().monday.do(job, name)
schedule.every().wednesday.at("13:15").do(job, name)

@repeat(every(3).seconds)
def task():
    now = datetime.now()
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    print(ts + '-333!')

while True:
    schedule.run_pending()
    time.sleep(1)

psutil

pids = psutil.pids()
for pid in pids:
    p = psutil.Process(pid)
    # get process name according to pid
    process_name = p.name()
    
    print("Process name is: %s, pid is: %s" %(process_name, pid))

可以查看的进程信息:

p.name()   #进程名
p.exe()    #进程的bin路径
p.cwd()    #进程的工作目录绝对路径
p.status()   #进程状态
p.create_time()  #进程创建时间
p.uids()    #进程uid信息
p.gids()    #进程的gid信息
p.cpu_times()   #进程的cpu时间信息,包括user,system两个cpu信息
p.cpu_affinity()  #get进程cpu亲和度,如果要设置cpu亲和度,将cpu号作为参考就好
p.memory_percent()  #进程内存利用率
p.memory_info()    #进程内存rss,vms信息
p.io_counters()    #进程的IO信息,包括读写IO数字及参数
p.connections()   #返回进程对象的列表
p.num_threads()  #进程开启的线程数p.username()  #执行用户的名

Win和Linux下杀死进程的方法不同

'''Windows'''
#根据进程名杀死进程
pro = 'taskill /f /im %s'% process_name
os.system(pro)
#根据pid杀死进程
process = 'taskill /f /pid %s'%pid
os.system(process)

'''linux'''
os.kill(pid, signal.SIGKILL)

Windows系统下定时检测无响应进程并重启

import os
import time

import schedule


def parse_output(output):
    print(output)
    pid_list = []
    lines = output.strip().split("\n")
    if len(lines) > 2:
        for line in lines[2:]:
            pid_list.append(line.split()[1])
    return pid_list


def list_not_response(process_name):
    return list_process(process_name, True)


def list_process(process_name, not_respond=False):
    cmd = 'tasklist /FI "IMAGENAME eq %s"'
    if not_respond:
        cmd = cmd + ' /FI "STATUS eq Not Responding"'
    output = os.popen(cmd % process_name)
    return parse_output(output.read())


def start_program(program):
    os.popen(program)


def check_job():
    process_name = "xx.exe"
    not_respond_list = list_not_response(process_name)
    if len(not_respond_list) <= 0:
        return
    pid_params = " ".join(["/PID " + pid for pid in not_respond_list])
    os.popen("taskkill /F " + pid_params)
    if len(list_process(process_name)) <= 0:
        start_program(r'E:\xxx\xx.exe')


if __name__ == '__main__':
    schedule.every(5).seconds.do(check_job)
    while True:
        schedule.run_pending()
        time.sleep(1)

Linux系统下检测无响应进程并重启

import subprocess
import os
import time

# 启动进程并返回进程ID
def start_process():
    p = subprocess.Popen(['python', 'your_script.py'])
    return p.pid

# 监听进程状态
def poll_process(pid, timeout=60):
    start_time = time.time()
    while time.time() - start_time < timeout:
        time.sleep(1)
        if os.kill(pid, 0) != 0:
            return False
    return True

# 重启进程
def restart_process():
    pid = start_process()
    return poll_process(pid)

# 主函数,每分钟检测一次进程状态
if __name__ == '__main__':
    pid = start_process()
    while True:
        if not poll_process(pid):
            print("Process not responding. Restarting...")
            os.kill(pid, signal.SIGTERM)
            pid = start_process()
        time.sleep(60)

其他检测程序无响应的思路

  • 比较CPU 和内存使用情况在两个时间点之间的差异以确定它们在这段时间内是否有响应
import psutil
import time

#获取要检测的进程
chrome_process = psutil.Process(2437)

#获取进程的 CPU 和内存使用情况
cpu_usage_before = chrome_process.cpu_percent(interval=1)
memory_usage_before = chrome_process.memory_percent()

#等待一段时间,再次获取
time.sleep(5)
cpu_usage_after = chrome_process.cpu_percent(interval=1)
memory_usage_after = chrome_process.memory_percent()

#比较两个时间点的 CPU 和内存使用情况,如果它们之间的差别很小,则说明进程在这段时间内保持响应状态。否则,进程可能已经出现了问题
if abs(cpu_usage_after - cpu_usage_before) < 1 and abs(memory_usage_after - memory_usage_before) < 1:
    print("进程正常工作。")
else:
    print("进程未响应。")

References

python练手脚本-定时检测无响应进程并重启 - 简书

python定时检测无响应进程并重启的实例代码 - Python技术站

 

你可能感兴趣的:(Python开发,python)