日志删除脚本

说明:需在脚本目录下创建文件log_arg.txt,第一列为日志路径,第二列日志名,第三列至少要保存的日志文件数量,第四列删除多少天之前的日志。


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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...