python - 如何检查列表中的任何进程是否正在运行?
问题描述
我需要检查是否有以下进程正在运行: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."
解决方案
你可以这样做:
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 概念的基础知识。
快乐学习!!
推荐阅读
- go - Go Struct 数据标识符/真实性
- python - Python错误地计算小浮点数和大数字
- reactjs - React dnd DragLayer 放置目标反馈
- python - 如何分别增加和减少模型精度和批量大小
- python - 在 Python 中临时将用户添加到 Active Directory 组
- reactjs - React-Select:如果输入被清除,则聚焦所选选项
- javascript - 根据变量生成范围内的数字
- r - R 和 RStudio 在 macOS 上运行缓慢
- javascript - JS Promises.all 问题
- tableau-api - 计算工具提示中多个单独度量值的列百分比