4

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!

alk
  • 69,737
  • 10
  • 105
  • 255
dylanoo
  • 45
  • 1
  • 2
  • 6
  • You should give examples of what you have tried so far and why it is not working. – Miguel-F Oct 23 '12 at 20:11
  • In the program I can use getpid() or getpgid() to get the pids and pgid of one program. Another try is with 'ps' command from link as 'http://linux.about.com/od/commands/l/blcmdl1_ps.htm' . – dylanoo Oct 23 '12 at 20:24
  • 1
    Yep, the `ps` command will give you the processes. You can then `grep` those results with what you are looking for. – Miguel-F Oct 23 '12 at 20:37
  • Yes, ps can do this. But this need me to identify the pids with same group id. What I want is something that can give me these pids with same group id, instead of hand select. do you have any idea about this? Thank you! – dylanoo Oct 23 '12 at 20:44
  • Do you want to do this in a program? Or using a shell tool? – Keith Nov 17 '12 at 00:44

4 Answers4

7

from man ps

To print a process tree:
      ps -ejH
      ps axjf

pstree can also help

Updated: Use pidof to find the process pids of the named programs. e.g. pidof chrome will get all chrome pids.

Edw4rd
  • 79
  • 4
3

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.

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • /proc only applies to Linux – seo Apr 24 '15 at 15:47
  • The **one more approach** answer you copied tried to copy a correct answer and incorrectly left off the `/s` modifier on the Perl regexp, and you copied their mistake here. Without the `/s` modifier on the Perl regexp, the `.` doesn't match newlines and so your answer doesn't actually work if there are newlines in the process file name and so your Perl answer is no better than the `awk` answer. Change `/g` to `/gs` at the end of the regexp and that's the right answer. – Ian D. Allen Jun 02 '22 at 03:11
0

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

  1. I'm assuming you know some Linux utilities already. This is written for bash shell only.
  2. 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.
  3. grep removes unnecessary header lines.
  4. 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 from A.
  5. For each line, we use awk to get the first value - this is the group ID, and the second value - this is the PID.
  6. If the group ID matches what you want, then print out the corresponding PID.
  7. 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.

laughing_man
  • 3,756
  • 1
  • 20
  • 20
-1

Based on man ps there are four parameters which deal with groups:

-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.

So you could get the group ID for your program using getpgrp [pid-of-your-program] then call ps -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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
TafT
  • 2,764
  • 6
  • 33
  • 51
  • 1
    You are confusing POSIX user groups and process groups. – mic_e May 21 '14 at 13:14
  • Thank you for the correction. I had not thought of it being the execution user group. So ps does not seem to offer the process group details. – TafT Jun 02 '14 at 09:40