-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkcpumem.sh
executable file
·58 lines (50 loc) · 1.08 KB
/
checkcpumem.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
#
# script checking CPU and memory utilization by user
#
#
var="";
inputuser="*"
hidden="no"
while getopts "hcmxu:" OPTION; do
case $OPTION in
c)
var="-k 2 -r"
;;
m)
var="-k 3 -r"
;;
u)
inputuser=$OPTARG
;;
x)
hidden="yes"
;;
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
if [ $hidden == "no" ]; then
echo ""
echo -e "user\t\tCPU\tMEM"
fi
# main loop
for user in `ps -eo uname:20,pid,pcpu,pmem,sz,tty,stat,time,cmd | grep -v UID | awk '{print $1}'| sort | uniq`;
do
if [ $user == "USER" ]; then
continue
fi
if [ $user == "$inputuser" ] || [ $inputuser == "*" ]; then
top -b -n 1 -u $user | awk -v var="$user" 'NR>7 { sumC += $9; }; { sumM += $10; } END { print var "\t\t" sumC "\t" sumM; }';
fi
done | sort -n `echo $var`