How to get pids in one process group on Linux

I have one question about Linux pid. How to get pids in one group? It seems easy to get all pids or pgid with the ps command on Linux, but how to get pids belonging to the same group, or, in other words, how to get pids of one program? Can someone please help me with this? Thanks!

+4
source share
4 answers

by man ps

 To print a process tree: ps -ejH ps axjf 

pstree can also help

Updated: use pidof to find pid processes of named programs. for example pidof chrome will get all chrome pids.

+7
source

All the other answers seem to be mentioned by ps , but no one is trying to directly access /proc .

There is another approach to Unix & Linux:

 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 a related answer.

+3
source

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 

Using

 ./test_script.sh (group ID you want to select for) 

Explanation

  • I assume that you know some Linux utilities already. This is written only for the bash shell.
  • ps -e -o pgid,pid= just prints all processes, with the first value of each line being its group identifier, and the second value being its process identifier, separated by a space.
  • grep removes unnecessary header lines.
  • IFS is a really important internal variable. This means that the string is limited. The for automatically limits the line to a space character, but if a new line is set for the IFS variable, this limits the use of this new space character. This ensures that each iteration variable is a string from A
  • For each row, we use awk to get the first value - this is the group identifier, and the second value is the PID.
  • If the group ID matches what you want, print the corresponding PID.
  • After you are done, you must disable IFS to its default value so that it does not linger with its changed state.

Notes

Hope this helps. It works for me. It is not very difficult as soon as you understand how awk and ps work. The rest is just parsing. If you want to pass the PID as an array, instead of printing it as a new line, just split it with something else and create a global string variable containing all the PIDs.

0
source

Based on man ps there are four parameters that relate to 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 can get the group id for your program using getpgrp [pid-of-your-program] , then call ps -G [group-if-of-your-program] .

Perhaps this is not the way you want. The process groups and the processes that make up the tree seem different. ppid is the parent pid of the process, you may need something that tells you all the pids with a given pid, like their ppid? I do not think there is anything to guarantee that it is the same that all pids are in the same process group, in fact, if there is only one process group for each process, they cannot be.

As suggested above, pstree should help you understand what is happening. The --show-pids option will also give you all the useful data that may be useful.

-1
source

Source: https://habr.com/ru/post/1441639/


All Articles