#!/bin/shexport TERM=xterm rm -rf top.infohome_dir=`pwd`cd $home_dirif [ ! -f $home_dir/system.info ];thentouch system.infoecho "day_tim|cpu_cost|mem_cost|swap_cost" >> system.infofi#top |head -5 >> top.infotop -bn 1|head -5|sed 's/ /abcdefg/g' > $home_dir/top.info#top -d 1 -n 1 -b|head -5|sed 's/ /abcdefg/g' > $home_dir/top.infotim(){day=`date +%Y-%m-%d`tim=`head -1 ${home_dir}/top.info|awk -F 'abcdefg|abcdefgabcdefg' '{print " "$3}'`day_tim=$day$tim}cpu(){cpuleft=`head -3 ${home_dir}/top.info|tail -1|awk -F 'abcdefg|abcdefgabcdefg' '{print $8}'`cpu_use=`echo 100 - $cpuleft|bc`cpu_cost=`echo "scale=2;$cpu_use/100"|bc`}mem(){mem_total=`head -4 ${home_dir}/top.info|tail -1|awk -F 'abcdefg|abcdefgabcdefg' '{print $3}'`mem_free=`head -4 ${home_dir}/top.info|tail -1|awk -F 'abcdefg|abcdefgabcdefg' '{print $7}'`mem_use=`echo "scale=2;$mem_total-$mem_free"|bc`mem_cost=`echo "scale=2;$mem_use/$mem_total"|bc`}swap(){swap_total=`tail -1 ${home_dir}/top.info|awk -F 'abcdefg|abcdefgabcdefg' '{print $3}'`swap_use=`tail -1 ${home_dir}/top.info|awk -F 'abcdefg|abcdefgabcdefg' '{print $8}'`swap_cost=`echo "scale=2;$swap_use/$swap_total"|bc`}zhuijia(){echo -n $day_tim"|" >> $home_dir/system.infoecho -n $cpu_cost"|" >> $home_dir/system.infoecho -n $mem_cost"|" >> $home_dir/system.infoecho $swap_cost >> $home_dir/system.inform -rf $home_dir/top.info}timcpumemswapzhuijia
此脚本需要设置定时任务crontab来时间记录