最近因为服务器上大量的带时间戳的日志存放过多,导致服务器上磁盘空间不够,因此就写了一个清理日志的脚本用于清理日志:
#!/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 {} \;
本文出自 “技术随笔” 博客,谢绝转载!
原文地址:http://jim123.blog.51cto.com/4763600/1912312