Tomcat 使用 jsvc 以守护进程的方式启动(daemon.sh )。这样tomcat自身将会生成另外一个日志文件(catalina-daemon.out),而不是之前的catalina.out,而且catalina-daemon.out日志不会自动切割,会越来越大。

以前遇到过一个问题,就是网站突然访问空白,排查到最后发现是当前进行了网站打包备份操作,有一个超过2GB的压缩包。删掉后立马页面访问正常。具体原因还不清楚。

同时从运维角度和日志分析角度思考,日志文件最好做切割处理,并日志文件不宜过大。

 

想了想,还是使用linux的crontab的定时任务吧,

编写一个shell脚本,脚本放到 /etc/cron.daily目录下,代码如下:

#!/bin/bash
thedate=`date --rfc-3339=date`
predate=`date +%Y-%m-%d --date="-7 day"`

rmfile="/xxxx/server/tomcat/logs/catalina-daemon.${predate}.out"
outfile="/xxxx/server/tomcat/logs/catalina-daemon.out"
if [ -f ${rmfile} ];then
   rm -f ${rmfile}
fi

if [ -f ${outfile} ];then
   cp ${outfile} /xxxx/server/tomcat/logs/catalina-daemon.${thedate}.out
   echo "" > ${outfile}
fi

exit 0
#!/bin/bash
thedate=`date --rfc-3339=date`
predate=`date +%Y-%m-%d --date="-7 day"`

rmfile1="/xxxxx/server/tomcat/logs/catalina-daemon.${predate}.out"
rmfile2="/xxxxx/server/tomcat/logs/p2p.log.${predate}"
rmfile3="/xxxxx/server/tomcat/logs/localhost.${predate}.log"
rmfile4="/xxxxx/server/tomcat/logs/host-manager.${predate}.log"
rmfile5="/xxxxx/server/tomcat/logs/catalina.${predate}.log"
outfile="/xxxxx/server/tomcat/logs/catalina-daemon.out"

for rmfile in "${rmfile1}" "${rmfile2}" "${rmfile3}" "${rmfile4}" "${rmfile5}"
do
    if [ -f ${rmfile} ];then
        rm -f ${rmfile}
    fi
done

if [ -f ${outfile} ];then
   cp ${outfile} /xxxxx/server/tomcat/logs/catalina-daemon.${thedate}.out
   echo "" > ${outfile}
fi

exit 0
View Code

相关文章: