在我的 Python 脚本中,我想检查是否otherscript.py
目前正在(Linux)系统上运行。这psutil http://psutil.readthedocs.io/en/latest/图书馆看起来是一个很好的解决方案:
import psutil
proc_iter = psutil.process_iter(attrs=["name"])
other_script_running = any("otherscript.py" in p.info["name"] for p in proc_iter)
问题是p.info["name"]
只给出进程的可执行文件的名称,而不是完整的命令。因此,如果python otherscript.py
在系统上执行,p.info["name"]
只会是python
对于该过程,我的脚本无法检测是否otherscript.py
是正在运行的脚本。
有没有一种简单的方法可以使用 psutil 或其他库进行此检查?我意识到我可以运行ps
命令作为子进程并查找otherscript.py
在输出中,但我更喜欢一个更优雅的解决方案(如果存在)。
我想知道这是否有效
import psutil
proc_iter = psutil.process_iter(attrs=["pid", "name", "cmdline"])
other_script_running = any("otherscript.py" in p.info["cmdline"] for p in proc_iter)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)