11

supervisord is running on CentOS server. If I do

ps -e -o %mem,%cpu,cmd | grep supervisord | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'

I get 0 0 just because supervisord is just an initialization daemon. It runs four child processes on my server:

# pgrep -P $(pgrep supervisord) | wc -l
4

How can I find summarized CPU and memory usage of these child processes in one-line-command?

Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
KennyPowers
  • 235
  • 1
  • 2
  • 8

3 Answers3

5

Given a pid,

pid=24535
pstree -p $pid | grep -o '([0-9]\+)' | grep -o '[0-9]\+' |\
  xargs ps -o %mem,%cpu,cmd -p | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'
# 15.5 905.2

I had no luck getting the pids of all child processes from pgrep.

Daniel
  • 151
  • 1
  • 3
  • 3
    %cpu in ps is, in fact, problematic. It is calculated as `CPU time used divided by the time the process has been running` - that is, historical average of the %CPU usage, not its current value. – VladV Oct 14 '16 at 12:29
5

The code from happyraul's answer,

pgrep -P $(pgrep supervisord) | xargs ps -o %mem,%cpu,cmd -p | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'

will get only one child layer.

If you want to search for all processes that were derived from a main pid, use this code:

ps -o pid,ppid,pgid,comm,%cpu,%mem  -u {user name} | {grep PID_PRINCIPAL}

The pid of main process is the PGID of child processes.

wjandrea
  • 658
  • 7
  • 19
jhonatan1090
  • 66
  • 1
  • 2
-1

Try using xargs:

pgrep -P $(pgrep supervisord) | xargs ps -o %mem,%cpu,cmd -p | awk '{memory+=$1;cpu+=$2} END {print memory,cpu}'
polym
  • 10,672
  • 9
  • 41
  • 65