2012-10-23 65 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

相關問題