Ubuntu: How to get the cpu utlization percentage for each user in ubuntu by a command



Question:

I've typed this command to get the memory usage for each user :

ps aux | awk 'NR>2{arr[$1]+=$6}END{for(i in arr) print i,arr[i]}'  

I want to know if there is a command or a way to get the cpu usage or in other words "cpu utilization percentage" for each user like the above command which its output :

enter image description here


Solution:1

I might be wrong, but isn't it just this ?

ps aux | awk 'NR>2{arr[$1]+=$3}END{for(i in arr) print i,arr[i]}'  

The response is in % of CPU usage.


Solution:2

I'm not good with scripts (this one for instance will certainly make some people cry and tear their eyes... feel free to edit my post to include such things as counter of other things I don't know how to do. )

This script will display the % of CPU used, calculating it from top. But it'll use a temporary file to store the usernames. I think this is the most appropriate way to calculate the CPU usage, since ps aux doesn't say exaclty the same as top.

#! /bin/bash    ## store the usernames in .aux.tmp temporary file  ps aux | awk 'NR>2{arr[$1]+=$6}END{for(i in arr) print i}' > .aux.tmp     ## print each user, then calcultate each CPU-usage with top  # this will only print the 9 firsts users. if you expect more than   # 9 users, add lines following the pattern (by replacing "9p"   # by "10p" for the 10st user, for example).    # print user 1 :  echo `sed -n '1p' .aux.tmp`   top -b -n 1 -u `sed -n '1p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'   echo ""    # print user 2 :  echo `sed -n '2p' .aux.tmp`    top -b -n 1 -u `sed -n '2p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 3 :  echo `sed -n '3p' .aux.tmp`    top -b -n 1 -u `sed -n '3p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 4 :  echo `sed -n '4p' .aux.tmp`    top -b -n 1 -u `sed -n '4p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 5 :  echo `sed -n '5p' .aux.tmp`    top -b -n 1 -u `sed -n '5p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 6 :  echo `sed -n '6p' .aux.tmp`    top -b -n 1 -u `sed -n '6p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 7 :  echo `sed -n '7p' .aux.tmp`    top -b -n 1 -u `sed -n '7p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 8 :  echo `sed -n '8p' .aux.tmp`    top -b -n 1 -u `sed -n '8p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""    # print user 9 :  echo `sed -n '9p' .aux.tmp`    top -b -n 1 -u `sed -n '9p' .aux.tmp`  | awk 'NR>7 { sum += $9; } END { print sum; }'  echo ""  

This script will print the 9 first users and the %CPU they use. You can add line if you have more than 9 users, just follow the same pattern (replace Xp by the number, twice per line)

You can copy-paste this in a file, and run it with bash /path/to/file


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »