首页 > 解决方案 > 如何检查列表中的任何进程是否正在运行?

问题描述

我需要检查是否有以下进程正在运行:script1.py、script2.py、script3.py

但是下面的示例仅检查其中一个进程是否正在运行。

import os
process_name= "script1.py"  # change this to the name of your process

tmp = os.popen("ps -Af").read()

if process_name not in tmp[:]:
    print "The process is not running."
else:
    print "The process is running."

标签: python

解决方案


你可以这样做:

import os 
process_names= ["script1.py","script2.py","script3.py"]
# you can modify this list as you want
tmp = os.popen("ps -Af").read()

for item in process_name: #this loop iterates through the list of script names
      if item not in tmp[:]: 
           print "The process is not running."
      else:
           print "The process is running."

但是,我强烈建议您在尝试此类中低复杂的用例之前学习 Python、循环和 oops 概念的基础知识。

快乐学习!!


推荐阅读