shell定时清理日志文件,只保留文件下最近的60个

最近发现服务器磁盘空间使用超过90%,查看原因是起得服务日志很长时间没清理导致的,所以写个脚本,定时清理日志文件,只保存logs文件下最近日期的60个日志文件。

脚本如下

# vim /testweb/shell/taskClean.sh

#!/bin/bash
DIR=/testweb
FILE_BAK=file_bak
# 定时清理日志,保留最近日期的60个文件
ls -1t $DIR/apache-tomcat-web/logs/*  | awk 'NR>60 {print "rm -f "$0}' | bash
ls -1t $DIR/apache-tomcat-manager/logs/*  | awk 'NR>60 {print "rm -f "$0}' | bash
ls -1t $DIR/apache-tomcat-uc/logs/*  | awk 'NR>60 {print "rm -f "$0}' | bash

创建定时任务

# crontab -e

# 每天凌晨,执行脚本,定时清理日志,保留最近日期的60个文件
0 0 * * * /bin/sh /testweb/shell/taskClean.sh

猜你喜欢

转载自blog.csdn.net/weixin_42170236/article/details/102966497
今日推荐