Bre*_*t38 3 python process psutil
在我的 Python 脚本中,我想检查otherscript.py当前是否正在(Linux)系统上运行。psutil库看起来是一个很好的解决方案:
import psutil
proc_iter = psutil.process_iter(attrs=["name"])
other_script_running = any("otherscript.py" in p.info["name"] for p in proc_iter)
Run Code Online (Sandbox Code Playgroud)
问题是p.info["name"]只给出进程的可执行文件的名称,而不是完整的命令。因此,如果python otherscript.py在系统上执行,p.info["name"]则仅适用于该进程,而我的脚本无法检测该脚本python是否正在运行。otherscript.py
有没有一种简单的方法可以使用 psutil 或其他库进行此检查?我意识到我可以将ps命令作为子进程运行并在输出中查找otherscript.py,但我更喜欢一种更优雅的解决方案(如果存在)。
小智 6
我想知道这是否有效
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5341 次 |
| 最近记录: |