Linux自动清理日志

首先创建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 <`date`> " > /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 <`date`>" > /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 <`date`>" > /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
<span style="white-space:pre">	</span>rm -rf $path$logFile;
<span style="white-space:pre">	</span>if [ 0 -eq $? ];then
<span style="white-space:pre">	</span>echo "clean log:$logFile done" >> /root/clean/tomcat.log;
<span style="white-space:pre">	</span>else
<span style="white-space:pre">	</span>echo "clean log:$logFile fail" >> /root/clean/tomcat.log;
<span style="white-space:pre">	</span>return 1;
<span style="white-space:pre">	</span>fi
fi

和cleanTomcatLog.sh 同目录下

放置到某个文件夹。
</pre><pre code_snippet_id="1643482" snippet_file_name="blog_20160412_5_5774672" name="code" class="html">然后在etc文件夹下找到crontab修改此文件
</pre><pre code_snippet_id="1643482" snippet_file_name="blog_20160412_7_7170895" name="code" class="html"><pre name="code" class="html"># /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 user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
<span style="color:#ff0000;">55 23	* * *	root	sh /root/cleanTomcatLog.sh</span>
#


 
新增标记类容(每天23点55分定时清理日志)然后此替换文件。
</pre><pre code_snippet_id="1643482" snippet_file_name="blog_20160412_10_7629026" name="code" class="html">修改完成之后使用  sudo /etc/init.d/cron restart 命令重启清理日志程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值