zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    javaweb web.xml文件详解
    Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
    系统环境搭建问题汇总
    从关系型数据库到非关系型数据库
    SpringMVC学习系列(3) 之 URL请求到Action的映射规则
    Spring MVC的实现原理
    谈谈对Spring IOC的理解
    hash算法 (hashmap 实现原理)
    为什么不能用两次握手进行连接?
    JVM内存管理和JVM垃圾回收机制
  • 原文地址:https://www.cnblogs.com/phpdragon/p/4255545.html
Copyright © 2011-2022 走看看