tomcat7catalina.out|tomcat7catalina.out 日志切割及定时清理

一.安装使用cronolog工具对catalina.out日志文件进行切割

#wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
2、解压缩
# tar zxvf cronolog-1.6.2.tar.gz
3、进入cronolog安装文件所在目录,运行安装
# cd cronolog-1.6.2
# ./configure && make && make install
4、查看cronolog安装后所在目录(验证安装是否成功)
# which cronolog
/usr/local/sbin/cronolog
二.使用cronolog工具对catalina.out日志文件进行切割
进入tomcat/bin目录,打开catalina.sh,修改一下内容
(1)if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
fi


############################################################改为:
if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out
fi
(2)touch "$CATALINA_OUT"
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi.......................................
-Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
else..............................................
【tomcat7catalina.out|tomcat7catalina.out 日志切割及定时清理】-Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"
fi
##############################################改为:
#touch "$CATALINA_OUT"
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
................................................
-Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
...........................................
-Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/local/sbin/cronolog "$CATALINA_OUT" >> /dev/null &
fi
三,结合sed命令,对多个tomcat进行操作
sed -i 's/touch "$CATALINA_OUT"/#touch "$CATALINA_OUT"/g' catalina.sh
sed -i 's; CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out; CATALINA_OUT="$CATALINA_BASE"/logs/catalina-%Y-%m-%d.out; g' catalina.sh
重启tamcat,查看logs目录
-rw-r--r-- 1 root root 2814661 Jan 23 14:25 catalina-2018-01-23.out
四.定时清理日志,删除大于十五天的日志,减少磁盘占用
#!/bin/bash
find /aebiz/soft/jenkins/logs/ -mtime +15 -exec rm {} \;
find /aebiz/soft/jobserver/logs/ -mtime +15 -exec rm {} \;
find /aebiz/soft/recommend/logs/ -mtime +15-exec rm {} \;
find /aebiz/soft/ydyscenter/logs/ -mtime +15-exec rm {} \;
添加到定时任务,每天23点10分执行
10 23 * * * sh /aebiz/soft/logclean.sh

    推荐阅读