生产环境中tomcat每天都会产生很多日志,如果不清理磁盘容量会不够,手动清理又太麻烦。因此写个脚本每天定时去删除5天前(根据实际情况而定)的日志文件。
1.写一个/usr/local/script/cleanTomcatlog.sh脚本
#!/bin/bash
export LANG=zh_CN
#tomcat1日志文件路径
export WEB_TOMCAT1=/usr/local/tomcat1/logs
#tomcat2日志文件路径
export WEB_TOMCAT2=/usr/local/tomcat2/logs
#tomcat3日志文件路径
export WEB_TOMCAT3=/usr/local/tomcat3/logs
echo > ${WEB_TOMCAT1}/catalina.out
echo > ${WEB_TOMCAT2}/catalina.out
echo > ${WEB_TOMCAT3}/catalina.out
find ${WEB_TOMCAT1}/* -mtime +5 -type f -exec rm -f {} \;
find ${WEB_TOMCAT2}/* -mtime +5 -type f -exec rm -f {} \;
find ${WEB_TOMCAT3}/* -mtime +5 -type f -exec rm -f {} \;
2.设置cleanTomcatlog.sh脚本可执行
chmod a+x cleanTomcatlog.sh
3.在控制台上输入以下命令
crontab -e
4.按i键编辑这个文本文件,输入以下内容,每天凌晨4:30重启tomcat
30 04 * * * /usr/local/script/cleanTomcatlog.sh
按esc键退出编辑,输入wq回车保存
5.重启定时任务
[root@]# service crond stop
[root@]# service crond start