linux新建定时任务清理tomcat日志

1,创建shell脚本文件

touch /home/bea1/apache-tomcat-7.0.81/logs/auto-del-15-days-ago-log.sh

2,修改文件权限

chmod +x /home/bea1/apache-tomcat-7.0.81/logs/auto-del-15-days-ago-log.sh

3,进入shell文件所在的目录,编辑shell脚本(输入vi命令后,打开文件,再点击i键,进入文件编辑状态)

cd /home/bea1/apache-tomcat-7.0.81/logs/
vi auto-del-15-days-ago-log.sh

4、编辑auto-del-15-days-ago-log.sh文件,输入以下内容

#!/bin/sh
find /home/bea1/apache-tomcat-7.0.81/logs -mtime +15 -name "manager.*.log" -exec rm -rf {} \;
find /home/bea1/apache-tomcat-7.0.81/logs -mtime +15 -name "localhost_access_log.*.txt" -exec rm -rf {} \;
find /home/bea1/apache-tomcat-7.0.81/logs -mtime +15 -name "localhost.*.log" -exec rm -rf {} \;
find /home/bea1/apache-tomcat-7.0.81/logs -mtime +15 -name "host-manager.*.log" -exec rm -rf {} \;
find /home/bea1/apache-tomcat-7.0.81/logs -mtime +15 -name "catalina.*.log" -exec rm -rf {} \;
echo " " > /home/bea1/apache-tomcat-7.0.81/logs/catalina.out;

5,点击ESC,然后输入:wq,保存退出
6,将auto-del-15-days-ago-log.sh执行脚本加入到系统计划任务,到点自动执行,输入以下内容,编辑定时任务文件

crontab -e

7,编辑、输入定时任务

10 0 15 * * /home/bea1/apache-tomcat-7.0.81/logs/auto-del-15-days-ago-log.sh >/dev/null 2>&1

8、保存退出,输入以下命令,查看定时文件(“-”后是小写的L)

crontab -l

注意:
1,‘mtime +15’指的是文件修改日期在15天之前的;
2,‘/home/bea1/apache-tomcat-7.0.81/logs’是我个人的tomcat日志路径,要根据实际换成自己的;
3,‘10 0 15 * * ’,意思是每月15日0时10分执行定时任务;
4,综上:这个定时任务就是:每月15日0时10分执行定时任务,删除文件的修改时间在15天之前的指定文件名的文件。

不足及错误之处,欢迎指正!

你可能感兴趣的:(linux新建定时任务清理tomcat日志)