点击查看 linux系统自带定时任务 cron 的介绍
- 创建脚本
vim clear_tomcat_logs.sh
- 编写脚本
#!/bin/bash
find $logs_path -mtime +7 -name "localhost.*.log" -exec rm -rf {} \;
find $logs_path -mtime +7 -name "localhost_access_log.*.txt" -exec rm -rf {} \;
find $logs_path -mtime +7 -name "catalina.*.log" -exec rm -rf {} \;
find $logs_path -mtime +7 -name "catalina.out.*" -exec rm -rf {} \;
find $logs_path -mtime +7 -name "manager.*.log" -exec rm -rf {} \;
find $logs_path -mtime +7 -name "host-manager.*.log" -exec rm -rf {} \;
>$logs_path/catalina.out;
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b9cfc42379ab1c247d4938ec83d16b86.png)
- 执行授权
chmod 744 clear_tomcat_logs.sh
ll
- 添加定时任务
crontab -e
0 5 */1 * * /bin/sh /opt/shell/clear_tomcat_logs.sh
- 检查 cron 是否启动
systemctl status crond
- 检查 cron 是否加入字启动
systemctl is-enabled crond.service