I have one question on Linux pid things. How to get pids in one same group? It seems easy to get all pids or pgid with 'ps' command in Linux, but how to get the pids that belong to same group, or in other words, how to get the pids of one same program? Anyone please give me some help on this? Thank you!
How to get pids in one process group in Linux OS
18.8k views Asked by dylanoo At
4
There are 4 answers
2
On
All other answers seem to mention ps
, but none tries to access /proc
directly.
On "Unix&Linux" there's one more approach:
awk '{print $5}' < /proc/$pid/stat
or, more safely,
perl -l -0777 -ne '@f = /\(.*\)|\S+/g; print $f[4]' /proc/$pid/stat
See details and comments in the linked answer.
0
On
I wrote a small script for this purpose.
Code
#!/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
Usage
./test_script.sh (group ID you want to select for)
Explanation
- I'm assuming you know some Linux utilities already. This is written for bash shell only.
ps -e -o pgid,pid=
simply prints out all the processes with the first value of each line being its group id, and the second value being its process ID, separated by a space.grep
removes unnecessary header lines.- IFS is a really important internal variable. What this does is regulate how a string is delimited. The
for
construct automatically delimits a string using a space character, but if the IFS variable is set to new line, this delimits using this new whitespace character. This ensures that each iteration variable is a line fromA
. - For each line, we use
awk
to get the first value - this is the group ID, and the second value - this is the PID. - If the group ID matches what you want, then print out the corresponding PID.
- After you are done, you must unset IFS to its default value so that it does not linger around with its changed state.
Remarks
I hope that helps. It works for me. It's not very complicated once you understand how awk and ps works. The rest is just parsing. If you want to pass the PIDs around as an array, instead of printing it as a new line, just delimit it using something else and make a global string variable that holds all the PIDs.
Based on
man ps
there are four parameters which deal with groups:So you could get the group ID for your program using
getpgrp [pid-of-your-program]
then callps -G [group-if-of-your-program]
.That might not be what you want though. Process Groups and processes that form a tree seem to be different things. ppid is the parent pid of a process, you might want something that tells you all the pids with a given pid as their ppid? I don't think there is anything to guarantee that is the same as all the pids being in the same process group, in fact if there is only one process group per-process they cannot be.
As suggested above,
pstree
should help you get an idea of what is going on. The--show-pids
option will give you all the pids as well which might be helpful.