当前位置:首页 > 服务器技术 > nginx

nginx日志切割脚本shell

 

nginx-log-rotate.sh

#!/bin/bash
#---------------------------------------------
# Comment:Used for rotating nginx log file.
# Author: alin
# Date: 09/27/2018
# Version: 1.0
#---------------------------------------------

LOG_FILE="*.log"
LOG_DIR=/data/log/nginx
LOG_SIZE=100
CURR_TIME=$(date +%Y%m%d%H%M)

for log in `ls $LOG_DIR/$LOG_FILE`
do
total_size=`stat -c %s $log`
size=$(($total_size/1024/1024))
if [ $size -gt $LOG_SIZE ]; then
############# copy
back_file_name="$log-$CURR_TIME"
cd $LOG_DIR
/usr/bin/cp $log $back_file_name
echo "" > $log
############# zip
if [[ $log =~ "8bet" ]]
then
log_file_dir=`ls $log | awk -F"/" '{print $5}' | awk -F"." '{print $1"."$2}'`
else
log_file_dir=`ls $log | awk -F"/" '{print $5}' | awk -F"." '{print $1}'`
fi
cd $log_file_dir
rename=`ls $back_file_name| awk -F"/" '{print $5}'`
zip -9q $rename".zip" $back_file_name
############# rm
find ./ -mtime +10 -name "*.zip" -exec rm -f {} ;
cd $LOG_DIR
rm -f $back_file_name
else
echo -e "33[32m[$log is less than 100M!]33[0m"
fi
done

echo -e "33[42mNginx-dir-logfile had been rolled successful!......33[0m"

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!