Skip to content

Commit 30404bb

Browse files
committed
clean show-cpu-and-memory.sh
1 parent 7ff96cf commit 30404bb

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

show-cpu-and-memory.sh

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@
66
# $ ./show-cpu-and-memory.sh
77
#
88
# @author Bryant Hang
9-
cur_date="`date +%Y%m%d`"
109

11-
total_mem="`free -m | grep 'Mem'`"
12-
total_cpu="`top -n 1 | grep 'Cpu'`"
10+
readonly cur_date="`date +%Y%m%d`"
1311

14-
echo '**********'$cur_date'**********'
15-
16-
echo ''
17-
18-
echo $total_mem
19-
echo $total_cpu
12+
readonly total_mem="`free -m | grep 'Mem'`"
13+
readonly total_cpu="`top -n 1 | grep 'Cpu'`"
2014

21-
echo ''
22-
23-
for i in `ps -ef | grep -v "awk|$0" | awk '{print $2}'`
24-
do
25-
# not pid
26-
if [[ $i == *[!0-9]* ]]; then
27-
continue
28-
fi
15+
echo '**********'$cur_date'**********'
16+
echo
17+
echo "total memory: $total_mem total cpu: $total_cpu"
18+
echo
2919

30-
mem=`cat /proc/$i/status 2> /dev/null | grep VmRSS | awk '{print $2" " $3}'`
31-
cpu=`top -n 1 -b | awk '$1=='$i'{print $9}'`
20+
for pid in `ps -ef | awk 'NR > 0 {print $2}'` ; do
21+
mem=`cat /proc/$pid/status 2> /dev/null | grep VmRSS | awk '{print $2 $3}'`
22+
cpu=`top -n 1 -b | awk -v "pid=${pid}" '$1==pid {print $9}'`
3223

33-
echo 'pid: '$i', memory: '$mem', cpu:'$cpu'%'
34-
done
24+
echo "pid: $pid, memory: $mem, cpu:$cpu%"
25+
done

0 commit comments

Comments
 (0)