1.编写删除日志脚本

1.1 创建文件

touch delLog.sh

1.2 编写脚本

echo  $(date +"%Y-%m-%d %H:%M:%S") "del file start....." >> del.log
find /usr/local/tomcat-7.0.72/logs/ -mtime +7 -exec rm -rf {} \;
echo "" > catalina.out
echo  $(date +"%Y-%m-%d %H:%M:%S") "delete logs sucess!" >> del.log

1.3 文件授权,改为可执行

chmod +X delLog.sh

2.配置crontab

2.1 编辑后保存

crontab -e

添加一行:

每天21:30执行任务

30 21 * * * sh /usr/local/task/delLog.sh

2.2查看运行日志

tail -f /var/log/cron

使用crontab执行任务自动删除保留7天内日志

 

 

 

至此,crontab定时删除服务器日志功能就完成啦。

相关文章:

  • 2021-11-25
  • 2021-06-03
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-06
  • 2021-06-26
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案