CPU and memory usage per user in linux

scripting

Finding CPU and memory utilization is one of the important tasks. Linux comes with various utilities for reporting. Following article contain shell script which will help you to find it out how much CPU or Memory is consuming every user.

At first we should explain how script was written.

At the beginning we use top command in batch mode.

top -b -n 1 -u <user>

Which will give us following output for example for wwwrun user:

# top -b -n 1 -u wwwrun
top - 02:27:40 up 16:16, 1 user, load average: 0.08, 0.06, 0.05
Tasks: 168 total, 1 running, 167 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.3%sy, 0.0%ni, 99.4%id, 0.2%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 502716k total, 473004k used, 29712k free, 28364k buffers
Swap: 1048572k total, 0k used, 1048572k free, 165192k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5663 wwwrun 20 0 134m 24m 3936 S 0.0 5.1 0:02.91 httpd2-prefork
5664 wwwrun 20 0 132m 23m 3876 S 0.0 4.9 0:02.41 httpd2-prefork
5666 wwwrun 20 0 133m 24m 3844 S 0.0 4.9 0:02.82 httpd2-prefork
5667 wwwrun 20 0 130m 20m 3680 S 0.0 4.2 0:01.05 httpd2-prefork
6044 wwwrun 20 0 129m 19m 3832 S 0.0 4.0 0:02.22 httpd2-prefork
6050 wwwrun 20 0 119m 9m 3844 S 0.0 2.0 0:01.49 httpd2-prefork
6680 wwwrun 20 0 132m 23m 3828 S 0.0 4.7 0:03.72 httpd2-prefork
6683 wwwrun 20 0 133m 23m 3844 S 0.0 4.9 0:02.46 httpd2-prefork
6684 wwwrun 20 0 133m 24m 3820 S 0.0 5.0 0:02.05 httpd2-prefork
6854 wwwrun 20 0 131m 21m 3788 S 0.0 4.4 0:01.74 httpd2-prefork

#

With this output we can easily count 9 and 10 column for CPU and memory as follow:

user=wwwrun; top -b -n 1 -u $user | awk -v var="$user" 'NR>7 { sumC += $9; }; { sumM += $10; } END { print var "\t\t" sumC "\t" sumM; }'

With output:

storage1:/depot/scripts/sh # user=wwwrun; top -b -n 1 -u $user | awk -v var="$user" 'NR>7 { sumC += $9; }; { sumM += $10; } END { print var "\t\t" sumC "\t" sumM; }'
wwwrun 0 44.17
storage1:/depot/scripts/sh #

And for final script I added options for sorting, -c by CPU and -m by memory:

#!/usr/bin/sh
#
# script checking CPU and memory utilization by user
#
#

var="";

while getopts "hcm" OPTION; do
case $OPTION in

c)
     var="-k 2 -r"
     ;;

m)
     var="-k 3 -r"
     ;;

h)
     echo "Usage:"
     echo "checkcpumem.sh -h "
     echo "checkcpumem.sh -c "
     echo "checkcpumem.sh -m "
     echo ""
     echo " -h show this help"
     echo " -c order by CPU usage, default by username"
     echo " -m order by memory usage, default by username"
     exit 0
     ;;

esac
done

# first line of tab
echo ""
echo -e "user\t\tCPU\tMEM"

# main loop
for user in `ps -ef | grep -v UID | awk '{print $1}'| sort | uniq`;
do
     top -b -n 1 -u $user | awk -v var="$user" 'NR>7 { sumC += $9; }; { sumM += $10; } END { print var "\t\t" sumC "\t" sumM; }';
done | sort -n `echo $var`

Final output:

# ./checkcpumem.sh -m

user CPU MEM
wwwrun 0 44.13
root 5.9 12.53
mysql 0 9.83
named 0 3.33
101 0 1.43
postfix 0 1.03
100 0 0.23
#

Leave a Reply