• 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——数据库操作
    【笔试】T实习生2014 总结
    【JS】Intermediate6:jQuery
    【JS】Intermediate5:Scope
    【JS】Intermediate4:JSON
    【JS】Intermediate3:AJAX
    【JS】Intermediate2:Events and Callbacks
    【JS】Intermediate1:The DOM
    【JS】Beginner9:Arrays
    【JS】Beginner8:Objects
  • 原文地址:https://www.cnblogs.com/chuyiwang/p/10619998.html
Copyright © 2020-2023  润新知