Tomcat catalina-deamon.out 日志切割 每天生成一个文件

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

 

检查是否配置好了自动执行配置 ,cat /etc/crontab,没有就加行下文的红色部分。

[root@xxxxServer /]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

#run-parts
01 * * * * root run-parts /etc/cron.hourly 
02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本 
22 4 * * 0 root run-parts /etc/cron.weekly 
42 4 1 * * root run-parts /etc/cron.monthly

 

查看crond服务是否运行:

pgrep crond

/sbin/service crond status

ps -elf|grep crond|grep -v "grep"

 

crond服务操作命令:

/sbin/service crond start //启动服务  
/sbin/service crond stop //关闭服务  
/sbin/service crond restart //重启服务  
/sbin/service crond reload //重新载入配置

 

设置crond随机启动

chkconfig crond on 235

 

好了,现在就不用担忧日志文件过大问题了!

同时上面的脚本会每天运行一次,并删除七天之前的日志文件,具体时间,可自己设定。

 

PS:

http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html

http://www.jb51.net/article/34332.htm

http://www.cnblogs.com/panblack/archive/2013/05/30/split_tomcat_catalina_out.html

http://desert3.iteye.com/blog/1393541

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值