加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

日志删除脚本

发布时间:2020-12-15 16:24:37 所属栏目:安全 来源:网络整理
导读:说明:需在脚本目录下创建文件log_arg.txt,第一列为日志路径,第二列日志名,第三列至少要保存的日志文件数量,第四列删除多少天之前的日志。 log_arg.txt 文件内容: # first: log dir # secound: log name # third: log file not less than # forth: dele

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读