Python regularly detects unresponsive processes and restarts (Win/Linux)

Optional kits:

  • schedule: Timing task scheduling library, if multi-threaded execution, be careful not to set it into an infinite loop
  • psutil: a cross-platform library that can easily obtain the running process and system utilization of the system

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))

Process information that can be viewed:

p.name() #process name
p.exe() #process bin path
p.cwd() #process working directory absolute path
p.status() #process status
p.create_time() #process creation time
p.uids () #process uid information
p.gids() #process gid information
p.cpu_times() #process cpu time information, including user and system two cpu information
p.cpu_affinity() #get process cpu affinity, if To set the cpu affinity, just use the cpu number as a reference
p.memory_percent() #Process memory utilization
p.memory_info() #Process memory rss, vms information
p.io_counters() #Process IO information, including reading and writing IO numbers and parameters
p.connections() #Return the list of process objects
p.num_threads() #The number of threads opened by the process p.username() #Execute the name of the user

The methods of killing processes under Win and Linux are different

'''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)

Periodic detection of unresponsive processes under Windows system and restart

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)

Detect unresponsive process and restart it under Linux system

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)

Other detection programs do not respond to ideas

  • Compare the difference in CPU and memory usage between two points in time to determine if they were responsive during that time
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 hand practice script - regular detection of unresponsive processes and restart - Programmer Sought

Example code of python timing detection of unresponsive process and restart - Python Technology Station

 

Guess you like

Origin blog.csdn.net/m0_64768308/article/details/131370633