zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    动态规划(最长公共序列,最长上升序列)
    滴滴笔试--算术转移(20190827)
    线段树和树状数组
    pair和list学习
    数据结构--树(建立、遍历)
    tmux常用命令与快捷键
    机器学习实战-逻辑回归
    字符流中第一个重复的字符
    机器学习实战-朴素贝叶斯
    Python第三方库
  • 原文地址:https://www.cnblogs.com/chuyiwang/p/10619998.html
Copyright © 2011-2022 走看看