1、做一個日志生成腳本,刪除30前的日志,按天依次刪除,中間如果未執行的情況,跳過的日期日志不會刪除。
#!/bin/bash
build_dir="/home/analysisLog/"
jar_dir="/opt/sms/server/sms-analysis/"
#判斷文件夾是否存在 -d
if [[ ! -d "$build_dir" ]]; then
mkdir -p $build_dir
fi
cp ${jar_dir}nohup.out ${build_dir}/`date -d "-1 day" "+%Y%m%d"`.out
echo "" > ${jar_dir}nohup.out
#要刪除日志的時間區間
content=$(date +%Y%m%d --date '1 month ago')
cd $build_dir
#計算刪除文件的數量
FileNum=$(find -name "*$content*".out|wc -l)
if [ "$FileNum" -gt 0 ];then
echo "delete [$content]"
rm -rf ./*${content}*.out
fi
2、crontab -e 輸入執行表達式 比如:1 0 * * * /home/linrui/XXXX.sh 每天00:01 執行。crontab -l查看有哪些定時器