1

I'm using the following command to gather the information of child-PID:

top -b -n1 -p 719011,719012,719013,719014,719015,719016,719017 | sed -e 's/USER/----/' -e 's/root/----/' -e 's/PR/--/' -e 's/20/--/' -e 's/VIRT/----/' -e 's/538m/----/' -e 's/RES/---/' -e 's/12m/---/' -e 's/SHR/---/' -e 's/1348/----/' -e 's/NI/--/' -e 's/S/-/' | tail -9 > file1

It works just fine, but is a little long winded! I want to know if there is anyway to gather the information just by using the pPID? Thus saving having to type every child process ID. I don't want the full answer just a pointer in the right direction so I can figure it out myself!

Okay, so I have tidied up the command to:

ps -o pid= --ppid 719008 | top -b -n4 | awk '{print $1,$9,$10,$12}' | tail -9 > file

But instead of giving the output for the child processes I get the output for every process on the system! What am I missing?

$ pstree -p 719008
dvaudio_server(719008)─┬─{dvaudio_server}(719011)
                       ├─{dvaudio_server}(719012)
                       ├─{dvaudio_server}(719013)
                       ├─{dvaudio_server}(719014)
                       ├─{dvaudio_server}(719015)
                       ├─{dvaudio_server}(719016)
                       └─{dvaudio_server}(719017)

And the output of pgrep is:

pgrep: option requires an argument -- 'd'
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
    [-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
  • top doesn't read PIDs from stdin. That's why I use -p $(...) in my answer. – muru Aug 24 '17 at 11:25
  • Please excuse my ignorance, I'm really very new to all this. if I,m understanding correctly the command should be thus : top -p $(pgrep -P 719008 -d,) | awk '{print $1,$9,$10,$12}' | tail -9 > file Or am I taking you to literally? – SimplySimplified Aug 24 '17 at 11:37
  • Yes, plus any additional options you want to give to top: top -b -p $(pgrep -P 719008 -d,) -n4 | awk .... – muru Aug 24 '17 at 11:39
  • When I run top -p with -n4 after the pgrep the result is 'bad pid -n4' and without -n4 the result is '-p argument missing' – SimplySimplified Aug 24 '17 at 11:47
  • So, again: the output of the pgrep and pstree commands you executed, please. – muru Aug 24 '17 at 11:49
2

You can obtain the direct children of a process using pgrep:

-P, --parent ppid,...
      Only match processes whose parent process ID is listed.

And:

-d, --delimiter delimiter
      Sets  the  string  used to delimit each process ID in the output
      (by default a newline).  (pgrep only.)

So:

top ... -p $(pgrep -P <PPID> -d,) | ...
  • When I use pgrep along with the top command I get error bad pid? Driving me mad!!!! – SimplySimplified Aug 23 '17 at 11:58
  • @SimplySimplified run the pgrep command by itself to see what output it gives. Have you used the correct parent PID? – muru Aug 23 '17 at 11:59
  • I get no output when running both pgrep and ps command you gave below. The ppid is correct, because when I run say a standard pstree -p PID it then brings up the process and it's tree! – SimplySimplified Aug 23 '17 at 12:09
  • @SimplySimplified which version of Ubuntu? – muru Aug 23 '17 at 12:16
  • I'm running 16.04 – SimplySimplified Aug 23 '17 at 12:21
2

You can use ps to display a list of processes. It understands the --ppid PPIDLIST argument which allows you to filter the processes by a comma-separated list of parent process IDs.

From man ps:

--ppid pidlist

Select by parent process ID. This selects the processes with a parent process ID in pidlist. 
That is, it selects processes that are children of those listed in pidlist. 

So probably you should try something like

ps aux --ppid 12345
  • ps -o pid= --ppid 12345, more like – muru Aug 23 '17 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.