How to read total CPU usage of the group of processes

I was wondering how to read total CPU usage of the group of processes. Then I came with very simple solution using just top and awk commands.

At first look at the command below to grasp an idea:

$ LC_ALL=C top -b -n1 -u $(whoami) | \
  awk 'NR==7;/chrome/ {print $0;SUM += $9} END \
       {printf "Total Google Chrome CPU usage: %.1f%%\n",SUM}'
  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM     TIME+ COMMAND
27077 milosz    20   0 1010m 124m  38m S  12.9  1.6   0:07.55 chrome
22581 milosz    20   0  876m 188m  57m S   6.5  2.4  15:59.30 chrome
  428 milosz    20   0  919m  63m  23m S   0.0  0.8   0:53.01 chrome
  792 milosz    20   0  878m  34m  16m S   0.0  0.4   0:00.83 chrome
  801 milosz    20   0  269m  18m  13m S   0.0  0.2   0:00.14 chrome
11069 milosz    20   0  947m  94m  25m S   0.0  1.2   0:13.34 chrome
22535 milosz    20   0  979m 202m  48m S   0.0  2.6  20:10.45 chrome
22540 milosz    20   0  251m 7348 2496 S   0.0  0.1   0:07.24 chrome
22541 milosz    20   0  6236  360  276 S   0.0  0.0   0:00.00 chrome-sandbox
22542 milosz    20   0  290m  21m  15m S   0.0  0.3   0:00.01 chrome
22546 milosz    20   0  306m 6732  780 S   0.0  0.1   0:00.26 chrome
22583 milosz    20   0  923m  89m  16m S   0.0  1.1   2:29.08 chrome
22707 milosz    20   0 1484m 113m  20m S   0.0  1.4  51:18.62 chrome
25371 milosz    20   0  947m 106m  25m S   0.0  1.3   0:12.03 chrome
26796 milosz    20   0  893m  44m  21m S   0.0  0.6   0:00.40 chrome
26943 milosz    20   0  981m 129m  24m S   0.0  1.6   0:04.41 chrome
29679 milosz    20   0  969m 107m  29m S   0.0  1.4   1:36.41 chrome
32655 milosz    20   0  919m  66m  23m S   0.0  0.8   0:45.56 chrome
Total Google Chrome CPU usage: 19.4%

Now extend it a little and create shell script:

#!/bin/sh

export LC_ALL=C

if [ "$#" -eq "0" ]; then
  echo "Simple script to display total CPU usage of group of processes"
  echo "Usage:   $0 group1 group2 group3 ..."
  echo "Example: $0 ^k ako\|blue ^top$"
else
  for group in $*; do
    top_group=`top -b -n1 -u $(whoami) | awk "\\$12 ~ /$group/  {if (NR>=7) { print \\$0}}"`
    processes=`echo "$top_group" | awk '{print $12}' | sort | uniq`

    echo "Group: " $group
    for process in $processes; do
     process_escaped=`echo $process | sed 's/\//\\\\\//g'`
     process_cpu_usage=`echo "$top_group" | awk "/${process_escaped}/ {SUM += \\$9} END {printf \"%.1f%%\",SUM}"`
     printf "%20s %s\n" "$process" "$process_cpu_usage"
    done
    total_group_cpu=`echo "$top_group" | awk "{SUM += \\$9} END {printf  \"%.1f%%\",SUM}"`
    printf "%20s %s\n" "Group total" "$total_group_cpu"
  done
fi

Sample script output:

$ cpu_group.sh chrome ako\|blue ^ko ^top$
Group:  chrome
              chrome 18.1%
      chrome-sandbox 0.0%
         Group total 18.1%
Group:  ako|blue
     akonadi_contact 0.0%
     akonadi_control 0.0%
     akonadi_ical_re 0.0%
     akonadi_maildir 0.0%
     akonadi_maildis 0.0%
       akonadiserver 0.0%
     bluedevil-helpe 0.0%
     bluedevil-monol 0.0%
         Group total 0.0%
Group:  ^ko
           konqueror 0.0%
             konsole 12.0%
              korgac 0.0%
         Group total 12.0%
Group:  ^top$
                 top 3.3%
         Group total 3.3%
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu