nginx日志切割

nginx优化之日志切割

编写日志分割的脚本

#!/bin/bash
#nginx日志切割
ACCESS_LOG=/var/log/nginx/access.log
ERROR_LOG=/var/log/nginx/error.log

NGINX=/usr/local/nginx/sbin/nginx
DATE=`date -d "-1 day" +%Y%m%d`
BAKUP_DIR=/bakup/logs/nginx

STATUS=`ps -ef |grep nginx |wc -l`

[ -d $BAKUP_DIR ] ||mkdir -p $BAKUP_DIR

if [ $STATUS -gt 1 ];then
    mv $ACCESS_LOG $BAKUP_DIR/access.log-$DATE
    mv $ERROR_LOG $BAKUP_DIR/error.log-$DATE
    $NGINX -s reopen
    gzip -9 $BAKUP_DIR/*
else
    echo "error: nginx is stoped." |tee -a $ERROR_LOG
fi

find $BAKUP_DIR -name "*.log*" -mtime +7 -exec rm -rf {} \;

将脚本添加到系统定时任务中

[root@lb nginx]# crontab -l
0    0    *    *    *    /root/logqg.sh
[root@lb nginx]#
[root@lb nginx]# chmod +x /root/logqg.sh

 

posted @ 2020-07-13 21:11  太阳的阳ฅ  阅读(125)  评论(0编辑  收藏  举报