最近因为服务器上大量的带时间戳的日志存放过多,导致服务器上磁盘空间不够,因此就写了一个清理日志的脚本用于清理日志:
#!/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 {} \;
时间: 2024-10-29 19:05:32