[font=楷体_GB2312]最近写一个shell 程序,其中有一个要求,就是要支持系统繁忙检测。若忙,则停止,若闲,则运行。
说实话,这种算法肯定比较多,以下是我个人的一个小算法。贴出来和大家分享。
我觉得挺不满意。贴出来的目的绝不是炫耀,这不是炫耀的地方,也不是炫耀的资本,而是希望和大家有所交流,希望看到更好的算法。我坚信有更好的算法出现。望大家多提意见和建议![/font]
function busy()
{
touch cpu.txt;
echo -e "\033[31m \n Detecting the %idle of CPU ... \033[0m ";
export temp=1;
sar -u 1 3 | awk '{print$9}' | tail -n 4 >>cpu.txt ;
tail -n 4 cpu.txt | cut -d "." -f 1 | while read var;
do
#echo $var;
if [[ $var -ge 20 ]];then
delete; ### Run into delete function
exit;
fi
temp=$[ $temp + 1 ];
if [ $temp -eq 4 ];then
echo -e "\n \033[37;5m So sorry, System is busy now ! \n\033[0m ";
fi
done;
rm -rf cpu.txt;
}
gzxyong 于 2012-10-21 18:48:35发表:
谢谢楼主!
于 2012-10-07 12:41:43发表:
楼主谦虚的精神值得学习
刘冲 于 2012-08-29 23:21:15发表:
欢迎技术帖啊,看了下,还是不错的。
dhwoecv 于 2012-07-23 21:26:58发表:
强