Skip to content

Instantly share code, notes, and snippets.

@zzh8829
Created February 8, 2017 23:25
Show Gist options
  • Save zzh8829/62fd8380ddd34a71b05921fce712d16c to your computer and use it in GitHub Desktop.
Save zzh8829/62fd8380ddd34a71b05921fce712d16c to your computer and use it in GitHub Desktop.
List cpu usage grouped by user
own=$(id -nu)
tmp=$(mktemp)
#for user in $(getent passwd | awk -F ":" '{print $1}' | sort -u)
for user in $(who | awk '{print $1}' | sort -u)
do
# print other user's CPU usage in parallel but skip own one because
# spawning many processes will increase our CPU usage significantly
if [ "$user" = "$own" ]; then continue; fi
(top -b -n 1 -u "$user" | awk -v user="$user" 'NR>7 { sum += $9; } END { print user, sum; }') >> $tmp &
done
wait
# print own CPU usage after all spawned processes completed
top -b -n 1 -u "$own" | awk -v user=$own 'NR>7 { sum += $9; } END { print user, sum; }' >> $tmp
cat $tmp | sort -k2nr | head -n20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment