Shell – How to Find CPU and Memory Usage of Child Processes

centoscpu usageprocesspsshell

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?

Best Answer

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.

Related Question