2012-10-23 71 views
4

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

+0

您应该举例说明迄今为止您尝试过的方法以及它为什么不起作用。 –

+0

在程序中,我可以使用getpid()或getpgid()来获得一个程序的pid和pgid。另一个尝试是将链接中的'ps'命令定义为'http://linux.about.com/od/commands/l/blcmdl1_ps.htm'。 – dylanoo

+1

是的,'ps'命令会为你提供进程。然后你可以用你正在寻找的东西来“grep”这些结果。 –

回答

7

man ps

To print a process tree: 
     ps -ejH 
     ps axjf 

pstree还可以帮助

更新:使用pidof找到指定的程序,进程的PID。例如pidof chrome将会获得所有的Chrome pid。

-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]然后调用ps -G [group-if-of-your-program]程序组ID。

这可能不是你想要的。形成树的进程组和进程似乎是不同的东西。 ppid是一个进程的父进程,你可能需要一些东西来告诉你所有的pid都带有一个给定的pid作为他们的ppid?我不认为有任何事情可以保证与同一个进程组中的所有pid相同,实际上,如果每个进程只有一个进程组,它们不可能是。

如上所述,pstree应该可以帮助您了解发生了什么。 --show-pids选项将为您提供所有可能有用的pid。

+1

您在混淆POSIX用户组和进程组。 –

+0

谢谢你的纠正。我没有想到它是执行用户组。所以ps似乎没有提供进程组的细节。 – TafT

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=只需打印出所有进程,每行的第一个值为其组标识,第二个值为进程标识,用空格分隔。
  3. grep删除不必要的标题行。
  4. IFS是一个非常重要的内部变量。它所做的是规定字符串是如何分隔的。 for构造会自动使用空格字符分隔字符串,但如果IFS变量设置为新行,则会使用此新的空白字符进行分隔。这确保每个迭代变量都是从A开始的一行。
  5. 对于每一行,我们使用awk获取第一个值 - 这是组ID,第二个值 - 这是PID。
  6. 如果组ID匹配你想要的,然后打印出相应的PID。
  7. 完成之后,您必须将IFS重置为其默认值,以便它不会随其状态发生变化。

备注

我希望帮助。这个对我有用。一旦你了解了awk和ps的工作方式,这并不是很复杂。其余的只是解析。如果你想以一个数组的形式传递PID,而不是将它作为一个新行打印出来,只需使用别的方法对其进行分隔,并创建一个全局字符串变量来存放所有的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 

见链接查看回答详情和评论。

+0

/proc只适用于Linux – seo

相关问题