【问题标题】:How to get pids in one process group in Linux OS如何在 Linux OS 中的一个进程组中获取 pid
【发布时间】:2012-10-13 20:08:57
【问题描述】:

我有一个关于 Linux pid 的问题。如何在同一组中获取pid? 在 Linux 中使用 'ps' 命令获取所有 pid 或 pgid 似乎很容易,但是如何获取属于同一组的 pid,或者换句话说,如何获取同一个程序的 pid? 有人请给我一些帮助吗?谢谢!

【问题讨论】:

  • 您应该举例说明您迄今为止尝试过的方法以及为什么它不起作用。
  • 在程序中我可以使用 getpid() 或 getpgid() 来获取一个程序的 pids 和 pgid。另一种尝试是使用链接中的 'ps' 命令作为 'linux.about.com/od/commands/l/blcmdl1_ps.htm' 。
  • 是的,ps 命令将为您提供进程。然后你可以grep那些你正在寻找的结果。
  • 是的,ps可以做到这一点。但这需要我识别具有相同组 ID 的 pid。我想要的是可以给我这些具有相同组 id 的 pid,而不是手动选择的东西。你对此有什么想法吗?谢谢!
  • 您想在程序中执行此操作吗?还是使用 shell 工具?

标签: linux pid process-group


【解决方案1】:

来自man ps

To print a process tree:
      ps -ejH
      ps axjf

pstree也可以帮忙

更新:使用pidof 查找指定程序的进程 pid。例如pidof chrome 将获取所有 chrome pid。

【讨论】:

    【解决方案2】:

    所有其他答案似乎都提到了ps,但没有人尝试直接访问/proc

    在“Unix&Linux”上有one more approach

    awk '{print $5}' < /proc/$pid/stat
    

    或者,更安全的是,

    perl -l -0777 -ne '@f = /\(.*\)|\S+/g; print $f[4]' /proc/$pid/stat
    

    在链接的答案中查看详细信息和 cmets。

    【讨论】:

    • /proc 仅适用于 Linux
    【解决方案3】:

    为此我写了一个小脚本。

    代码

    #!/bin/bash 
    MY_GROUP_ID=$1
    A="$(ps -e -o pgid,pid= | grep [0-9])"
    #printf "$A\n"
    IFS=$'\n'
    for i in $A; do
        GROUP_ID=$(printf "$i" | awk -F ' ' '{print $1}')
        PID=$(printf "$i" | awk -F ' ' '{print $2}')
        if [ "$GROUP_ID" = "$MY_GROUP_ID" ]; then
            printf "$PID\n"
        fi
    done
    unset IFS
    

    用法

    ./test_script.sh (group ID you want to select for)
    

    说明

    1. 我假设您已经知道一些 Linux 实用程序。这是为 bash shell 编写的。
    2. ps -e -o pgid,pid= 简单地打印出所有进程,每行的第一个值是它的组 ID,第二个值是它的进程 ID,用空格分隔。
    3. grep 删除了不必要的标题行。
    4. IFS 是一个非常重要的内部变量。它的作用是规范字符串的分隔方式。 for 构造自动使用空格字符分隔字符串,但如果 IFS 变量设置为换行符,则使用这个新的空白字符进行分隔。这可确保每个迭代变量都是来自A 的一行。
    5. 对于每一行,我们使用awk 获取第一个值 - 这是组 ID,第二个值 - 这是 PID。
    6. 如果组 ID 与您想要的匹配,则打印出相应的 PID。
    7. 完成后,您必须将 IFS 取消设置为其默认值,这样它就不会在更改后的状态下徘徊。

    备注

    我希望这会有所帮助。这个对我有用。一旦您了解了 awk 和 ps 的工作原理,这并不是很复杂。剩下的只是解析。如果您想将 PID 作为数组传递,而不是将其打印为新行,只需使用其他内容对其进行分隔并创建一个包含所有 PID 的全局字符串变量。

    【讨论】:

      【解决方案4】:

      基于man ps,有四个处理组的参数:

      -G grplist
            Select by real group ID (RGID) or name.  This selects the processes whose real group name or ID is in the grplist list.  The real group ID identifies the group of the user who created the process, see getgid(2).
      
      -g grplist
            Select by session OR by effective group name.  Selection by session is specified by many standards, but selection by effective group is the logical behavior that several other operating systems use.  This ps will select by session when the list is
            completely numeric (as sessionsare).  Group ID numbers will work only when some group names are also specified.  See the -s and --group options.
      
      --Group grplist
            Select by real group ID (RGID) or name.  Identical to -G.
      
      --group grplist
            Select by effective group ID (EGID) or name.  This selects the processes whose effective group name or ID is in grouplist.  The effective group ID describes the group whose file access permissions are used by the process (see getegid(2)).  The -g
            option is often an alternative to --group.
      

      因此您可以使用getpgrp [pid-of-your-program] 获取程序的组ID,然后调用ps -G [group-if-of-your-program]

      这可能不是你想要的。形成树的进程组和进程似乎是不同的东西。 ppid 是进程的父 pid,您可能想要告诉您以给定 pid 作为其 ppid 的所有 pid 的东西?我认为没有什么可以保证与同一进程组中的所有 pid 相同,事实上,如果每个进程只有一个进程组,它们就不可能。

      如上所述,pstree 应该可以帮助您了解正在发生的事情。 --show-pids 选项也会为您提供所有可能有用的 pid。

      【讨论】:

      • 您混淆了 POSIX 用户组和进程组。
      • 感谢指正。我没想到它是执行用户组。所以 ps 似乎没有提供进程组的详细信息。
      猜你喜欢
      • 1970-01-01
      • 2012-09-15
      • 2011-07-11
      • 2023-03-11
      • 2016-07-23
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多