tomcat的catalina.out日志切割

Tomcat服务在启动后会在$CATALINA_HOME/logs目录下生成catalina.out日志文件,随着时间的推移 日志会越来越大,查看起来不太方便,严重影响到系统维护工作。下面来介绍一下该日志的切割工作,让该 日志按照日期每天进行切割:

 

依赖组件及时间的更新

[root@localhost ~]# yum -y install gcc ntpdate wget
[root@localhost ~]# ntpdate time.nist.gov
[root@localhost ~]# echo '* */4 * * * /usr/sbin/ntpdate -s time.nist.gov' >> /var/spool/cron/root

 

cronolog安装

[root@localhost ~]# wget http://down1.chinaunix.net/distfiles/cronolog-1.6.2.tar.gz
[root@localhost ~]# tar -xf cronolog-1.6.2.tar.gz
[root@localhost ~]# cd cronolog-1.6.2
[root@localhost cronolog-1.6.2]# ./configure --prefix=/usr/local/cronolog
[root@localhost cronolog-1.6.2]# make && make install

 

tomcat日志切割

[tomcat@localhost tomcat7_8080]$ pwd
/app/tomcat7_8080
[tomcat@localhost tomcat7_8080]$ vi bin/catalina.sh
将
touch "$CATALINA_OUT"	
改为
#touch "$CATALINA_OUT"(注释该行,默认是不注释的,大概在370行)

将下面说明部分
  else
    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
      -Dcatalina.base="\"$CATALINA_BASE\"" \
      -Dcatalina.home="\"$CATALINA_HOME\"" \
      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
      org.apache.catalina.startup.Bootstrap "$@" start \ (更改这行,大概在392行)
      >> "$CATALINA_OUT" 2>&1 "&"(更改这行,大概在393行)

  fi

  if [ ! -z "$CATALINA_PID" ]; then 
    echo $! > "$CATALINA_PID"
  fi


改为
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/cronolog/sbin/cronolog "$CATALINA_OUT"_%Y%m%d >>/dev/null &

 

重启tomcat使配置生效

[tomcat@localhost tomcat7_8080]$ bin/shutdown.sh
[tomcat@localhost tomcat7_8080]$ bin/startup.sh