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