如何在 Linux 上使用 Python 检查进程是否仍在运行? [英] How to check if a process is still running using Python on Linux?
本文介绍了如何在 Linux 上使用 Python 检查进程是否仍在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现的唯一好的方法是:
import sys
import os
try:
os.kill(int(sys.argv[1]), 0)
print "Running"
except:
print "Not running"
(来源)
但这可靠吗?它是否适用于每个流程和每个发行版?
(Source)
But is this reliable? Does it work with every process and every distribution?
推荐答案
Mark 的回答是要走的路,毕竟这就是/proc 文件系统存在的原因.对于一些更多的复制/粘贴:
Mark's answer is the way to go, after all, that's why the /proc file system is there. For something a little more copy/pasteable:
>>> import os.path
>>> os.path.exists("/proc/0")
False
>>> os.path.exists("/proc/12")
True
这篇关于如何在 Linux 上使用 Python 检查进程是否仍在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文