zoukankan      html  css  js  c++  java
  • 实现Nginx的日志分割!

    时间越久内存占用率越高,高并发量,会给服务器带来巨大的缓存压力

    解决方法:创建一个新的目录,设置周期性计划定期的将旧目录里面的日志移动到新目录中,一般只保存30天内,30天后一律删除

    [root@localhost ~]# date "+%Y%m%d"                                  //今天的时间
    20190913
    [root@localhost ~]# date -d "-1 day" "+%Y%m%d"                //昨天的时间
    20190912

    =====================================================================================

    [root@localhost ~]# vim /opt/cut_nginx_log.sh

    复制代码
    #!/bin/bash
    #cut_nginx_log.sh
    
    datetime=$(date -d "-1 day" "+%Y%m%d")        //时间,date -d "-1 day"代表日期减1
    log_path="/usr/local/nginx/logs"             //日志的存放位置
    pid_path="/usr/local/nginx/logs/nginx.pid"   //进程的PID号,有PID号代表 进程还活着
    [ -d $log_path/backup ] || mkdir -p $log_path/backup   //如果$log_path/backup不是一个目录则创建$log_path/backup目录
    if [ -f $pid_path ]                                //如果$pid_path是一个文件
    then
    mv $log_path/access.log $log_path/backup/access.log-$datetime  //那么移动旧日志的文件到新目录下以此来实现分割
    kill -USR1 $(cat $pid_path)                                    //USR1代表信号,他会给进程传一个信号让进程生成一个新的日志
    find $log_path/backup -mtime +30 | xargs rm -f                //查找30天前的日志并删除
    else
    echo "Error,Nginx is not working!" | tee -a /var/log/messages  //否则提示nginx is not working且追加到日志中
    fi
    复制代码

    [root@localhost ~]# chmod +x /opt/cut_nginx_log.sh
    [root@localhost ~]# bash /opt/cut_nginx_log.sh
    [root@localhost ~]# cd /usr/local/nginx/logs/
    [root@localhost logs]# ls
    access.log   backup  error.log  nginx.pid
    [root@localhost logs]# ls backup/
    access.log-20190912

    [root@localhost ~]# crontab -e
    crontab: installing new crontab
    [root@localhost ~]# crontab -l
    0    0   *    *    *     bash /opt/cut_nginx_log.sh            //每月每周每天的0时0分执行脚本     

    当未运行时!

    [root@localhost logs]# killall -9 nginx
    [root@localhost logs]# bash /opt/cut_nginx_log.sh
    /opt/cut_nginx_log.sh: 第 11 行:kill: (8282) - 没有那个进程
    [root@localhost logs]# rm -rf /usr/local/nginx/logs/nginx.pid
    [root@localhost logs]# bash /opt/cut_nginx_log.sh
    Error,Nginx is not working!

  • 相关阅读:
    echarts折线图
    利用echarts制作词云
    本周总结
    本周总结
    云服务器项目数据库连接超时问题解决
    iOS下载图片失败
    解决后台json数据返回的字段需要替换的问题
    设置User Agent
    Xcode升级到9.3之后pod问题
    gitLab创建自己的私有库
  • 原文地址:https://www.cnblogs.com/L1-5551/p/11518567.html
Copyright © 2011-2022 走看看