linux 定时任务,压缩 日志,并删除掉 指定日期之前的 日志


sh文件

#!/bin/sh
 myPath="/var/www/Client/storage/logs/"
 myFile="lumen.log"
 
 cd $myPath
 
 date=$(date +%Y%m%d)
 
 find ${myPath} -type f -name "*.gz" -mtime +30 -exec rm {} \;
 #这里的-f参数判断$myFile是否存在
 if [ -e "$myFile" ]; then
    mv $myFile ${myFile%.*}${date}.${myFile##*.}
    tar -zcvf ${myFile%.*}${date}.log.tar.gz ${myFile%.*}${date}.${myFile##*.}
    rm -rf $myPath/${myFile%.*}${date}.${myFile##*.}
 fi

  只要定时任务中添加即可,注意 要是同时运行 ,多个脚本的压缩命令。 不要在一个时间 进行 ,会导致 压缩失败


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM