Linux下用cronolog切割Tomcat日志并删除指定天数前的日志记录
使用cronolog工具切分Tomcat的catalina.out日志文件
普通用户下载、安装cronolog
[tomcat@localhost ~]$ tar -xf cronolog-1.6.2.tar.gz
[tomcat@localhost ~]$ cd cronolog-1.6.2/
[tomcat@localhost cronolog-1.6.2]$ ./configure --prefix=/home/tomcat/cronolog
make
make install
root用户下下载、安装cronolog
[root@localhost ~]# rpm -qa |grep cronolog
[root@localhost ~]# tar zxvf cronolog-1.6.2.tar.gz
[root@localhost ~]# cd cronolog-1.6.2
[root@localhost cronolog-1.6.2]# ./configure
[root@localhost cronolog-1.6.2]# make && make install
[root@localhost cronolog-1.6.2]# which cronolog
/usr/local/sbin/cronolog
在普通用户的家目录下编辑.bash_profile文件里添加“$HOME/cronolog/sbin”添加cronolog的安装路径
[tomcat@localhost ~]$ vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:$HOME/cronolog/sbin
export PATH
用which cronolog可以查到安装的路径,这个路径待会在修改catalina.sh时会用到。
修改catalina.sh 文件
将186行【CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out】修改成187行【CATALINA_OUT="$CATALINA_BASE"/logs/opres.%Y-%m-%d.out】内容,日志名可以自行定义;
注释掉370行【touch "$CATALINA_OUT"】;
383、384行【
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"】替换成385、386行【
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /home/tomcat/cronolog/sbin/cronolog "$CATALINA_OUT" >> /dev/null 2>&1 &
】;
396、397行【
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
】替换成394、395行【
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /home/tomcat/cronolog/sbin/cronolog "$CATALINA_OUT" >> /dev/null 2>&1 &
】;最后一步(394、395)要注意如果原本的命令只是注释掉,替换的内容一定要在原本内容的上面,不然无法正常启动,前面的替换(383、384)位置没有关系的
97 JAVA_OPTS="-Xms512m -Xmx1024m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m"
98
99 # OS specific support. $var _must_ be set to either true or false.
185 if [ -z "$CATALINA_OUT" ] ; then
186 # CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
187 CATALINA_OUT="$CATALINA_BASE"/logs/opres.%Y-%m-%d.out
370 #touch "$CATALINA_OUT"
371 if [ "$1" = "-security" ] ; then
372 if [ $have_tty -eq 1 ]; then
373 echo "Using Security Manager"
374 fi
375 shift
376 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
377 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
378 -Djava.security.manager \
379 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
380 -Dcatalina.base="\"$CATALINA_BASE\"" \
381 -Dcatalina.home="\"$CATALINA_HOME\"" \
382 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
383 # org.apache.catalina.startup.Bootstrap "$@" start \
384 # >> "$CATALINA_OUT" 2>&1 "&"
385 org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
386 | /home/tomcat/cronolog/sbin/cronolog "$CATALINA_OUT" >> /dev/null 2>&1 &
387
388 else
389 eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
390 -Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
391 -Dcatalina.base="\"$CATALINA_BASE\"" \
392 -Dcatalina.home="\"$CATALINA_HOME\"" \
393 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
394 org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
395 | /home/tomcat/cronolog/sbin/cronolog "$CATALINA_OUT" >> /dev/null 2>&1 &
396 # org.apache.catalina.startup.Bootstrap "$@" start \
397 # >> "$CATALINA_OUT" 2>&1 "&"
398 fi
可以通过./catalina.sh start 来查看配置文件是否正确;这样在/usr/local/tomcat/logs每天会自动生成opres.%Y-%m-%d.out文件,下面我们要做的是定期清理这些过期的文件,我们可以通过crontab来实现
[tomcat@localhost ~]$ crontab -e
30 3 * * * /bin/find /home/tomcat/openapi/logs/ -mtime +7 -type f -name "opres*.*.out" -exec /bin/rm -f {} \;
[tomcat@localhost ~]$ crontab -l
30 3 * * * /bin/find /home/tomcat/openapi/logs/ -mtime +7 -type f -name "opres*.*.out" -exec /bin/rm -f {} \;
每天凌晨3点半执行清除7天之前过期日志