【问题标题】:How to get process status using pid?如何使用pid获取进程状态?
【发布时间】:2011-07-20 19:53:35
【问题描述】:

如果我知道一个进程的 pid,我如何使用 Python 判断该进程是否是僵尸进程?

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以使用psutil 中的status 功能:

    import psutil
    p = psutil.Process(the_pid_you_want)
    if p.status == psutil.STATUS_ZOMBIE:
        ....
    

    【讨论】:

    • p.status() 应该被使用(至少在 python 3 中)
    【解决方案2】:

    这里有一个使用 procfs 的快速破解(假设您使用的是 Linux):

    def procStatus(pid):
        for line in open("/proc/%d/status" % pid).readlines():
            if line.startswith("State:"):
                return line.split(":",1)[1].strip().split(' ')[0]
        return None
    

    这个函数应该为僵尸返回'Z'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      相关资源
      最近更新 更多