首先创建cleanTomcatLog.sh 清理tomcat2-3-4的日志
#!/bin/sh
echo "--------------------tomcat2:`date +%Y%m%d`-----------------------" >> /root/clean/tomcat.log
sh /root/cleanLog.sh /mnt/tomcat2/logs/ catalina.20*
sh /root/cleanLog.sh /mnt/tomcat2/logs/ localhost.20*
sh /root/cleanLog.sh /mnt/tomcat2/logs/ host-manager.20*
sh /root/cleanLog.sh /mnt/tomcat2/logs/ manager.20*
sh /root/cleanLog.sh /mnt/tomcat2/logs/ work*
if [ -f "/mnt/tomcat2/logs/catalina.out" ];then
echo "clean catalina.out log " > /mnt/tomcat2/logs/catalina.out
else
echo "catalina.out not found,need restart tomcat1"
fi
echo "-----------------------------------------------------------------" >> /root/clean/tomcat.log
echo "--------------------tomcat3:`date +%Y%m%d`-----------------------" >> /root/clean/tomcat.log
sh /root/cleanLog.sh /usr/sdb/tomcat3/logs/ catalina.20*
sh /root/cleanLog.sh /usr/sdb/tomcat3/logs/ localhost.20*
sh /root/cleanLog.sh /usr/sdb/tomcat3/logs/ host-manager.20*
sh /root/cleanLog.sh /usr/sdb/tomcat3/logs/ manager.20*
sh /root/cleanLog.sh /usr/sdb/tomcat3/logs/ work*
if [ -f "/mnt/tomcat3/logs/catalina.out" ];then
echo "clean catalina.out log " > /mnt/tomcat3/logs/catalina.out
else
echo "catalina.out not found,need restart tomcat3"
fi
echo "-----------------------------------------------------------------" >> /root/clean/tomcat.log
echo "--------------------tomcat4:`date +%Y%m%d`-----------------------" >> /root/clean/tomcat.log
sh /root/cleanLog.sh /mnt/tomcat4/logs/ catalina.20*
sh /root/cleanLog.sh /mnt/tomcat4/logs/ localhost.20*
sh /root/cleanLog.sh /mnt/tomcat4/logs/ host-manager.20*
sh /root/cleanLog.sh /mnt/tomcat4/logs/ manager.20*
sh /root/cleanLog.sh /mnt/tomcat4/logs/ work*
if [ -f "/mnt/tomcat4/logs/catalina.out" ];then
echo "clean catalina.out log " > /mnt/tomcat4/logs/catalina.out
else
echo "catalina.out not found,need restart tomcat4"
fi
echo "-----------------------------------------------------------------" >> /root/clean/tomcat.log
创建cleanLog.sh
#!/bin/sh
path="$1";
logFile="$2";
if [ "" = $path ];then
echo "require path" >> /root/clean/tomcat.log;
else
rm -rf $path$logFile;
if [ 0 -eq $? ];then
echo "clean log:$logFile done" >> /root/clean/tomcat.log;
else
echo "clean log:$logFile fail" >> /root/clean/tomcat.log;
return 1;
fi
fi
和cleanTomcatLog.sh 同目录下
放置到某个文件夹。
然后在etc文件夹下找到crontab修改此文件
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow usercommand
17 ** * *root cd / && run-parts --report /etc/cron.hourly
25 6* * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6* * 7roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 61 * *roottest -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
55 23* * *rootsh /root/cleanTomcatLog.sh
#
新增标记类容(每天23点55分定时清理日志)然后此替换文件。
修改完成之后使用 sudo /etc/init.d/cron restart 命令重启清理日志程序。