微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

日志删除脚本

说明:需在脚本目录下创建文件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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐