您的位置:首页 > 其它

自动清理带时间戳的log脚本

2017-04-01 10:15 351 查看
最近因为服务器上大量的带时间戳的日志存放过多,导致服务器上磁盘空间不够,因此就写了一个清理日志的脚本用于清理日志:
#!/bin/bash
#logs_clenup.sh:Used to clean up logs
#00 00 1 * * /usr/local/scripts/logs_clenup.sh
#writer jiangtao
#histor
#2017.3.31
########PATH########
app_dir=/usr/local/scripts/text_app_dir
logs_dir=/data/logs/clrnup_logs
two_month_ago=$(date -d '-2 month' +%Y-%m)
datetime=$(date +"%Y-%m-%d")
datetime_dir=$logs_dir/"$datetime"
logs_dir_path=$(find $app_dir -type d -name 'logs' -print)

if [ ! -e "$datetime_dir" ];then
mkdir -p $datetime_dir
fi
########PATH########
echo "============================ start cleanup logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log

for i in $logs_dir_path
do
rm -rf $i/*$two_month_ago*
echo "`date` $i cleanup success" | tee -a $datetime_dir/logs_clenup_${datetime}.log
done

if [ $? -eq 0 ];then
echo "============================ end cleanup logs `date +"%Y-%m-%d %H:%M:%S"`" | tee -a $datetime_dir/logs_clenup_${datetime}.log
else
echo "cleanup logs fail!" | tee -a $datetime_dir/logs_clenup_${datetime}.log
fi

find $logs_dir -type f -ctime +30 -name "logs_clenup_*" -exec rm -vf {} \;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  logs cleanup