删除6天以前的日志:
find ./ -type f -mtime +6 -name "*.out" -exec rm -f {} ;
参考:https://www.cnblogs.com/peida/archive/2013/03/25/2980121.html
https://www.rxblog.xyz/linux-rm-crontab/
find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} ;
1,创建脚本,并分配权限
touch /opt/soft/bin/auto-del-30-days-ago-log.sh chmod +x auto-del-30-days-ago-log.sh
2,编辑脚本
vi auto-del-30-days-ago-log.sh 编辑auto-del-30-days-ago-log.sh文件如下: #!/bin/sh find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} ; ok,保存退出(:wq)。
3,添加任务
①执行:
crontab -e 将auto-del-30-days-ago-log.sh执行脚本加入到系统计划任务,到点自动执行 ②输入: 10 0 * * * /opt/soft/log/auto-del-7-days-ago-log.sh >/dev/null 2>&1 这里的设置是每天凌晨0点10分执行auto-del-7-days-ago-log.sh文件进行数据清理任务了
4,查询任务
crontab -l
#!/bin/bash #Clear tomcat expiration log #ENV WORKDATE="`date '+%Y-%m-%d.%H:%M:%S'`" #删除Tomcat日志,参数(天数)没传值时默认为60 if [ ! -n "$1" ]; then day=60 else day=$1 fi #################### tomcat logs clear #################### TOMCAT_DIR=/usr/local/tomcat LOG_DIR=$TOMCAT_DIR/logs find $LOG_DIR/catalina.* -type f -mtime +$day -exec rm {} ; find $LOG_DIR/host-manager.* -type f -mtime +$day -exec rm {} ; find $LOG_DIR/localhost.* -type f -mtime +$day -exec rm {} ; find $LOG_DIR/localhost_access_log.* -type f -mtime +$day -exec rm {} ; find $LOG_DIR/manager.*.log -type f -mtime +$day -exec rm {} ; find $TOMCAT_DIR/webapps/logs/all.log.* -type f -mtime +$day -exec rm {} ; find $TOMCAT_DIR/webapps/logs/error.log.* -type f -mtime +$day -exec rm {} ; #################### tomcat logs clear #################### TOMCATF_DIR=/usr/local/tomcatadmin LOGF_DIR=$TOMCATF_DIR/logs find $LOGF_DIR/catalina.* -type f -mtime +$day -exec rm {} ; find $LOGF_DIR/localhost.* -type f -mtime +$day -exec rm {} ; find $LOGF_DIR/localhost_access_log.* -type f -mtime +$day -exec rm {} ; find $LOGF_DIR/manager.*.log -type f -mtime +$day -exec rm {} ; #find $TOMCATF_DIR/webapps/logs/all.log.* -type f -mtime +$day -exec rm {} ; #find $TOMCATF_DIR/webapps/logs/error.log.* -type f -mtime +$day -exec rm {} ; #crontab -e Add: 1 0 * * * sh /usr/local/sbin/logClear_Tomcat.sh exit
#!/bin/sh find /usr/local/tomcat-mail-provider-8081/logs/ -mtime +0 -name "*.log" -exec rm -rf {} ; find /usr/local/tomcat-mail-provider-8081/logs/ -mtime +0 -name "*.txt" -exec rm -rf {} ; find /usr/local/tomcat-mail-api-8082/logs/ -mtime +0 -name "*.log" -exec rm -rf {} ; find /usr/local/tomcat-mail-api-8082/logs/ -mtime +0 -name "*.txt" -exec rm -rf {} ;