【问题标题】:Programmatically getting list of child processes of a given PID以编程方式获取给定 PID 的子进程列表
【发布时间】:2023-04-02 03:50:01
【问题描述】:

我想获取给定 PID 的所有直系子级的列表。我可以使用/proc,但/proc/<PID>/task/<PID>/children 并不精确,可能会返回不准确的结果(see section 3.7 here)。我想要一个更可靠的方法来做到这一点。

我不希望在 shell 命令周围使用包装器。

【问题讨论】:

    标签: python node.js linux ubuntu process


    【解决方案1】:

    为什么不使用 psutils?

    这是一个我杀死所有孩子的例子。

    def infanticide(pid):
        try:
          parent = psutil.Process(pid)
        except psutil.NoSuchProcess:
          return
        children = parent.children(recursive=True)
        for p in children:
            os.kill(p.pid, signal.SIGKILL)
    

    【讨论】:

    • Nodejs 有一个不错的端口吗?我可以找到一些尝试,但没有完成。
    • 对不起,我爱上了 python 标签。在我的 Node.js 上不行。
    猜你喜欢
    • 2011-09-07
    • 2010-12-04
    • 1970-01-01
    • 2011-04-28
    • 2015-11-15
    • 2020-08-31
    • 2013-03-13
    • 2021-08-08
    相关资源
    最近更新 更多