Advertisement
peetaur

Quick and dirty listing of CPU used, grouped by process

May 30th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.85 KB | None | 0 0
  1. #!/bin/bash
  2. # Quick and dirty listing of CPU used, grouped by process with the same name (maybe different pid or even path!)
  3.  
  4. IFS=$'\n'
  5. foundheader=0
  6.  
  7. unset foundNames
  8. declare -A foundNames
  9.  
  10. for line in $(top -b -n 1); do
  11.     if echo "$line" | grep "PID.*USER.*PR"; then
  12.         foundheader=1
  13.         continue
  14.     fi
  15.     if [ $foundheader = 0 ]; then
  16.         continue
  17.     fi
  18.  
  19.     #echo "line is $line"
  20.     name=$(echo "$line" | awk '{print $12}')
  21.     cpu=$(echo "$line" | awk '{print $9}')
  22.  
  23.     oldCpu=${foundNames["$name"]}
  24.     let newCpu=cpu+oldCpu
  25.  
  26.     #echo "name $name, cpu $cpu, oldCpu $oldCpu, newCpu $newCpu"
  27.  
  28.     foundNames["$name"]="$newCpu"
  29. done
  30.  
  31. for key in ${!foundNames[@]}; do
  32.     value=${foundNames[$key]}
  33.     if [ "$value" = 0 ]; then
  34.         continue
  35.     fi
  36.     printf "%2d%% %s\n" "${value}" "$key"
  37. done | sort -rn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement