微服务应用日志清理脚本
此脚本应用于spring cloud微服务应用日志清理
脚本内容:
cat > /app/sh/log_rorate.sh <<EOF #!/bin/sh function rotate() { logs_path=$1 echo Rotating Log: $1 hour1=$(date -d -1hour +%Y-%m-%d-%H) echo "`date +'%F %T'` 开始备份out文件:${logs_path}" cp ${logs_path} ${logs_path}.$hour1 #备份目录 mkdir -p /app/logs/archive mv ${logs_path}.$hour1 /app/logs/archive/ #压缩备份日志 /bin/gzip -q /app/logs/archive/*.out.* echo "`date +'%F %T'` 开始清空out文件:${logs_path}" > ${logs_path} echo "`date +'%F %T'` 清空完成" #删除一个月前的应用备份日志 /bin/rm -f /app/logs/archive/*.$(date -d "30 days ago" +"%Y-%m-%d")* } for i in `find /app/logs/ -size +0 -name '*.out'` do rotate $i done EOF
计划任务:
0 * * * * /bin/bash /app/sh/log_rorate.sh > /app/logs/log_rorate.log 2>&1
find命令清理:
#清理超过30天的日志 /bin/find /app/logs/ -type f -name "*.log" -ctime +30 -exec rm -f {} \;