How to find the cpu and memory usage of child processes

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.


the code

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 u 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 processe is the PGID of child processes.