说明:需在脚本目录下创建文件log_arg.txt,第一列为日志路径,第二列日志名,第三列至少要保存的日志文件数量,第四列删除多少天之前的日志。
# first: log dir
# secound: log name
# third: log file not less than
# forth: delete log how many days ago
/glassfish/dm02n02s02/dm02c03s02/logs server.log_\* 20 7
/glassfish/dm02n02s02/dm02c03s04/logs server.log_\* 20 7
脚本rm_log.sh内容:
#!/bin/bash
dn=`dirname $0`
cd $dn
awk '/^[^#]/' log_arg.txt | while read log
do
logarr=($log)
len=${#logarr[@]}
dir=${logarr[0]}
fn=${logarr[1]}
cnt=${logarr[2]}
dt=${logarr[3]}
if [ $len != 4 ]; then
echo "Line \"$log\" is error"
continue
fi
[[ ! -d $dir ]] && echo "$dir is not exist" && continue
[[ ! "$cnt" =~ ^[0-9]+$ ]] && echo "$cnt is not a number" && continue
[[ ! "$dt" =~ ^[0-9]+$ ]] && echo "$dt is not a number" && continue
acnt=`find $dir -type f -name $fn -ctime -$dt | wc -l`
echo "$dir $acnt $cnt"
if [ $acnt -ge $cnt ] ; then
echo "Now delete $dt days ago logs in $dir. Log files $fn not less than $cnt."
find $dir -type f -name $fn -ctime +$dt | xargs rm -f
fi
done