如何在Linux上使用Python检查进程是否仍在运行? [英] How to check if a process is still running using Python on Linux?

查看:876
本文介绍了如何在Linux上使用Python检查进程是否仍在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的唯一 nice 方法是:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆