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
只要定時任務中添加即可,注意 要是同時運行 ,多個腳本的壓縮命令。 不要在一個時間 進行 ,會導致 壓縮失敗