如何在 Linux 上的 Python 中检查进程是否处于活动状态? [英] How do I check if a process is alive in Python on Linux?

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

问题描述

我在 Python 中有一个进程 ID.我知道我可以用 os.kill() 杀死它,但是我如何检查它是否还活着?是否有内置函数或者我必须去shell?

I have a process id in Python. I know I can kill it with os.kill(), but how do I check if it is alive ? Is there a built-in function or do I have to go to the shell?

推荐答案

使用 subprocess 模块来生成进程.有 proc.poll() 函数 - 如果进程还活着,它返回 None,否则返回进程返回码.

Use subprocess module to spawn process. There is proc.poll() function - it returns None if process is still alive, otherwise it returns process returncode.

http://docs.python.org/library/subprocess.html

这篇关于如何在 Linux 上的 Python 中检查进程是否处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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