• Nginx日志分割脚本


    脚本说明:

    每天00:00定时切割Nginx日志

    [root@check1 ~]# cat /etc/crontab 
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    
    # For details see man 4 crontabs
    
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # | .------------- hour (0 - 23)
    # | | .---------- day of month (1 - 31)
    # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
    # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # | | | | |
    # * * * * * user-name command to be executed
    00 00 * * * root /bin/bash /usr/local/nginx/conf/cut_nginx_log.sh

    使用下面脚本,需根据实际路径进行修改

    修改下面三项即可:

    • OLD_FILE中 +90代表保留90天的日志,90天后的会自动清理
    • LOGS_PATH
    • PID_PATH
    #!/bin/bash
    #CUT_NGINX_LOG
    #Version:1.0
    LOGS_PATH="/usr/local/nginx/logs"
    YESTERDAY=`date -d "yesterday" +"%Y-%m-%d"`
    PID_PATH="/usr/local/nginx/logs/nginx.pid"
    OLD_FILE=`find $LOGS_PATH -mtime +90 -type f -name "ERROR*"`
    
    if [ ! -d "$LOGS_PATH/old_access" ]
    then
        mkdir $LOGS_PATH/old_access
    fi
    if [ ! -d "$LOGS_PATH/old_error" ]
    then
        mkdir $LOGS_PATH/old_error
    fi
    
    mv ${LOGS_PATH}/access.log ${LOGS_PATH}/old_access/ACCESS_${YESTERDAY}.log
    mv ${LOGS_PATH}/error.log ${LOGS_PATH}/old_error/ERROR_${YESTERDAY}.log
    
    kill -USR1 `cat ${PID_PATH}`
    
    if [[ ! -z $OLD_FILE ]]
    then
        for a in $OLD_FILE
        do
            TIME=`date`
            echo "delet $a $TIME" >> /var/log/messages
            rm -rf $a
        done
    fi
  • 相关阅读:
    python学习第三课
    Linux基本操作知识一
    Linux是什么
    Django 多对多 关系
    Django 解决跨域
    支付宝支付
    django中间件
    DRF一对多序列化和反序列化
    python代码实现购物车(django的redis与vue)
    Spring 依赖注入两种方式
  • 原文地址:https://www.cnblogs.com/chuyiwang/p/10619998.html
Copyright © 2020-2023  润新知