如何获取Linux操作系统中某个进程组的pid

问题描述 投票:0回答:5

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

linux pid process-group
5个回答
7
投票

来自

man ps

To print a process tree:
      ps -ejH
      ps axjf

pstree
也可以帮忙

更新:使用

pidof
查找指定程序的进程 pid。例如
pidof chrome
将获取所有 chrome pid。


3
投票

所有其他答案似乎都提到了

ps
,但没有人尝试直接访问
/proc

在“Unix&Linux”上还有另一种方法

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

或者,更安全的是,

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

请参阅链接答案中的详细信息和评论。


0
投票

我为此目的写了一个小脚本。

代码

#!/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 的全局字符串变量即可。


0
投票

我推荐

pgrep

给定一个进程组id

$pgid
,打印出同一进程组中的所有进程:

pgrep -g $pgid

输出如下:

77341
77373
86475

您还可以使用

-l
选项列出进程名称:

pgrep -g $pgid -l

有关更多信息,请参阅 pgrep 手册页


-1
投票

基于

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,这可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.