这一博文,是对上一博文的中两个脚本的细节的分析,内容不多,但是价值不小!
其一,就是CPU的统计利用率,我采用的是top指令。计算用户态和内核态的使用率之和。
function cpuSts(){
content=`top -b -n 1| grep "Cpu(s)"`
##echo $content
cpup=`echo ${content#*:}`
cpuu=`echo ${cpup%us*}`
cpuss=`echo ${cpup#*,}`
cpus=`echo ${cpuss%sy*}`
us=`echo "$cpuu" | sed 's/^[ \t]*//g'`
sy=`echo "$cpus" | sed 's/^[ \t]*//g'`
#echo $us
ius=`echo $us |awk -F ' ' '{print $2}'`
#echo $ius
#echo $sy
isy=`echo $sy |awk -F ' ' '{print $2}'`
#echo $isy
fius=`echo $ius |awk -F '%' '{print $1}'`
fisy=`echo $isy |awk -F '%' '{print $1}'`
#total=`echo $fius+$fisy | bc`
#total=$(echo "$total * 100" | bc)
total=`echo "$fius $fisy" | awk '{printf("%0.2f\n",$1+$2)}'`
#total=`echo "$total * 100" | bc`
total=`echo "$total 100" | awk '{printf("%0.2f\n",$1*$2)}'`
echo "$total"
}
top参数不带-b的情况下,cpuSts不能作为后台程序被调度执行。否则会爆出下面的错误:
[tomcat@p0-tkonline-css-web01 ~]$ tailf nohup.out
top: failed tty get
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
另外,还有一点,关于上述的脚本,就是浮点数运算,在后台进程中进行时,不能用bc指令,我们可以采用awk的函数。因为bc在后台脚本运行时,会爆出下面的错误:
[tomcat@p0-tkonline-css-web01 ~]$ tailf nohup.out
(standard_in) 1: syntax error
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
https://www.cnblogs.com/zhutouli/p/12124017.html