某个目录日志文件越来越大,定期将日志清理掉,以免撑爆服务器。
另外写日志时精简,去掉不必要的日志;
limit-log.sh:控制日志的行数,超过就删掉之前的。
[monitor]$ more ~/bin/limit-log.sh
#/bin/sh
function limitlog(){
local logfile=$1
local maxline=$2
linecount=`/usr/bin/wc -l $logfile|awk '{print $1}'`;
echo file:$logfile, line count=$linecount. line limit=$maxline.
if [ ${linecount} -gt ${maxline} ];then
delcount=`expr ${linecount} - ${maxline}`;
echo delcount=$delcount
sed -i "1,${delcount}d" $logfile
echo $delcount lines deleted
else
echo do nothing.
fi
}
#handle monitor.dat
date
limitlog /home/monitor/log/monitor.dat 10000000
#handle sar.log
limitlog /home/monitor/log/sar.log 20000000
#handle redisinfo.log
date
limitlog /home/monitor/log/redisinfo.log 50000000
date
echo #############################
其中 sar.log redisinfo.log monitor.dat是要限制的日志文件, 后面的数字是行数。