Python脚本检查如果一个特定的Linux命令仍在运行

问题描述 投票:1回答:1

我想写一个Python脚本,将检查每分钟,如果一些预先定义的过程仍在Linux机器上运行,如果它不打印在它崩溃什么时间的时间戳。我写了一个脚本这是做正是但不幸的是,它只有一个进程正常工作。

这是我的代码:

import subprocess
import shlex
import time
from datetime import datetime

proc_def = "top"
grep_cmd = "pgrep -a " + proc_def
try:
    proc_run = subprocess.check_output(shlex.split(grep_cmd)).decode('utf-8')
    proc_run = proc_run.strip().split('\n')
    '''
    Creating a dictionary with key the PID of the process and value
    the command line
    '''
    proc_dict = dict(zip([i.split(' ', 1)[0] for i in proc_run],
                         [i.split(' ', 1)[1] for i in proc_run]))

    check_run = "ps -o pid= -p "

    for key, value in proc_dict.items():
        check_run_cmd = check_run + key
        try:
            # While the output of check_run_cmd isn't empty line do
            while subprocess.check_output(
                                          shlex.split(check_run_cmd)
                                          ).decode('utf-8').strip():
                # This print statement is for debugging purposes only
                print("Running")
                time.sleep(3)
        '''
        If the check_run_cmd is returning an error, it shows us the time
        and date of the crash as well as the PID and the command line
        '''
        except subprocess.CalledProcessError as e:
            print(f"PID: {key} of command: \"{value}\" stopped
                  at {datetime.now().strftime('%d-%m-%Y %T')}")
            exit(1)
# Check if the proc_def is actually running on the machine
except subprocess.CalledProcessError as e:
    print(f"The \"{proc_def}\" command isn't running on this machine")

例如,如果有两个top处理它会告诉我有关信息只有这些过程之一的碰撞时间,它就会退出。我想保持活跃,只要有另一个进程正在运行和退出只有两个进程被杀死。当每个进程的崩溃应该呈现信息。

它也将不限于两个PROC只,并支持多进程开始与同proc_def命令。

python linux subprocess
1个回答
1
投票

要改变逻辑一点,但基本上你想要一个无限循环上的所有进程交替检查 - 不一遍又一遍检查同一个:

import subprocess
import shlex
import time
from datetime import datetime

proc_def = "top"
grep_cmd = "pgrep -a " + proc_def
try:
    proc_run = subprocess.check_output(shlex.split(grep_cmd)).decode('utf-8')
    proc_run = proc_run.strip().split('\n')
    '''
    Creating a dictionary with key the PID of the process and value
    the command line
    '''
    proc_dict = dict(zip([i.split(' ', 1)[0] for i in proc_run],
                         [i.split(' ', 1)[1] for i in proc_run]))

    check_run = "ps -o pid= -p "
    while proc_dict:
        for key, value in proc_dict.items():
            check_run_cmd = check_run + key
            try:
                # While the output of check_run_cmd isn't empty line do
                subprocess.check_output(shlex.split(check_run_cmd)).decode('utf-8').strip()
                # This print statement is for debugging purposes only
                print("Running")
                time.sleep(3)
            except subprocess.CalledProcessError as e:
                print(f"PID: {key} of command: \"{value}\" stopped at {datetime.now().strftime('%d-%m-%Y %T')}")
                del proc_dict[key]
                break
# Check if the proc_def is actually running on the machine
except subprocess.CalledProcessError as e:
    print(f"The \"{proc_def}\" command isn't running on this machine")

这从原始代码相同的问题受到影响,即时间分辨率为3秒,如果这个脚本在运行一个新的进程,你会不会ping到它(尽管这可能需要)。

第一个问题将通过休眠时间不足,这取决于你所需要的是固定的,第二个通过运行初始线路中的proc_dict创造while True

© www.soinside.com 2019 - 2024. All rights reserved.