CentOS 编译安装Cronolog轮转Apache和Tomcat日志

cronolog稳定版本为 1.6.2,但此版本有一个弊端:当日志文件的大小到2G(2150726147字节)时,cronolog进程就变成僵尸进程,即日志无法再写入,cronolog 1.7 版本解决了这个问题。

下载

# cd /usr/local/src
# wget http://cronolog.org/patches/cronolog-1.7.0-beta.tar.gz

安装

# tar zxvf cronolog-1.7.0-beta.tar.gz
# cd cronolog-1.7.0
# ./configure --prefix=/usr/local/cronolog
# make
# make install

修改Apache虚拟主机配置

将虚拟主机中日志一行修改为如下

CustomLog "|/usr/local/cronolog/sbin/cronolog /www/log/apache/www.xiayige.org_%Y%m%d.log" combined

修改Tomcat配置

修改tomcat的bin目录下的catalina.sh

# vim /usr/local/tomcat/bin/catalina.sh

找到如下代码

  touch "$CATALINA_OUT"
  if [ "$1" = "-security" ] ; then
    if [ $have_tty -eq 1 ]; then
      echo "Using Security Manager"
    fi
    shift
    "$_RUNJAVA" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_OUT" 2>&1 &

  else
    "$_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 \
      >> "$CATALINA_OUT" 2>&1 &

  fi

将最上面的touch “$CATALINA_OUT”注释掉如下

#touch "$CATALINA_OUT"

将下面两处

org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 &

修改为

org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/cronolog/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &

然后重启tomcat即可