简体   繁体   English

遍历进程列表以检查 PID 是否存在于 Python 子进程中

[英]Iterating through list of processes to check if PID exists with Python subprocess

I'm creating a Python program to monitor the processes on a server once an hour, to see if it can return the PID.我正在创建一个 Python 程序来每小时监视一次服务器上的进程,以查看它是否可以返回 PID。 To do this, I made a function that uses subprocess to call pgrep -f on any name submitted to it.为此,我创建了一个函数,该函数使用 subprocess 对提交给它的任何名称调用 pgrep -f。 If it returns a process, the function evaluates to true;如果它返回一个进程,则该函数的计算结果为真; otherwise, it returns false.否则,它返回 false。

import subprocess
import psutil


def check_essentials(name):
    child = subprocess.Popen(['pgrep', '-f', name], stdout=subprocess.PIPE, shell=False)
    response = child.communicate()[0]
    pid = response.split()
    if len(pid) == 0:
        print("unable to find PID")
        return False
    else:
        print("PID is %s" % pid)
        return True

essentialApps = ['ProfileService','aflaf']
sendEmail=False

for x in essentialApps:
    check_essentials(x)
    if check_essentials == False:
        print("Unable to find PID for %s. Sending email alert" % x)
        sendEmail = True
    else:
        print("Found PID for %s" % x)

I then set up a for loop to have it iterate through a list of process names( essentialApps ) and see if it can return anything for them.然后我设置了一个 for 循环,让它遍历进程名称列表( essentialApps ),看看它是否可以为它们返回任何东西。 If not, sendEmail is set to true.如果不是,则 sendEmail 设置为 true。

In testing this, however, I'm finding that the else statement is always being called, regardless of whether or not the app exists.但是,在对此进行测试时,我发现始终会调用 else 语句,无论该应用程序是否存在。 When I call this program ( python alert.py ), I get the following output:当我调用这个程序( python alert.py )时,我得到以下输出:

PID is [b'11111']
Found PID for ProfileService 
unable to find PID #This is expected
Found PID for aflaf #This should be "Unable to find PID for aflaf"

I'm sure it is something simple, but can anyone tell me why it is not evaluating check_essential correctly?我确定这很简单,但是谁能告诉我为什么它没有正确评估 check_essential ?

Also, is there anyway to do this with psutil?另外,有没有办法用psutil做到这一点? I'm reading that this should be used over subprocess, but I'm not able to find anyway to specifically mimic pgrep -f name or ps -aux | grep name我读到这应该用于子ps -aux | grep name ,但无论如何我都找不到专门模仿pgrep -f nameps -aux | grep name ps -aux | grep name . ps -aux | grep name Thats important, as I have multiple Java applications running on the machine, and the program name that psutil seemed to see was always 'java', not 'ProfileService'.这很重要,因为我在机器上运行了多个 Java 应用程序,而 psutil 似乎看到的程序名称始终是“java”,而不是“ProfileService”。

You're not using the result of the function, you're checking if the check_essentials function itself is False .您没有使用函数的结果,而是在检查check_essentials函数本身是否为False

Which it's not, because it's a function.它不是,因为它是一个函数。

You need to you the result of check_essentials in your condition, check_essentials is always True , because it is a Python object:您需要在您的条件中check_essentials的结果, check_essentials始终为True ,因为它是一个 Python 对象:

for x in essentialApps:
    check_result = check_essentials(x)
    if check_result == False:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM