zoukankan      html  css  js  c++  java
  • 常用日志维护脚本

    日志格式

    2015/01/03 22:13:53 [error] 30310#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    2015/01/03 22:15:54 [notice] 31090#0: signal process started
    2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    2015/01/03 22:19:51 [notice] 32601#0: signal process started
    2015/01/03 22:19:51 [error] 32601#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    2015/01/03 23:01:51 [notice] 4960#0: signal process started
    2015/01/04 13:22:05 [notice] 4644#0: signal process started
    2015/01/04 21:08:07 [notice] 7135#0: signal process started
    2015/01/11 20:59:24 [notice] 5201#0: signal process started
    2015/01/12 23:31:29 [notice] 5121#0: signal process started

    1. 打印大于2015年1月3号的日志

    shell> gawk -F ' ' '{if($1>"2015/01/03"){print $0}}' /usr/local/nginx/logs/error.log
    
    2015/01/04 13:22:05 [notice] 4644#0: signal process started
    2015/01/04 21:08:07 [notice] 7135#0: signal process started
    2015/01/11 20:59:24 [notice] 5201#0: signal process started
    2015/01/12 23:31:29 [notice] 5121#0: signal process started

    2. 打印1月3号到1月12号之间的日志

    shell> gawk -F ' ' '{if($1>"2015/01/03"&&$1<"2015/01/12"){print $0}}' /usr/local/nginx/logs/error.log 
    
    2015/01/04 13:22:05 [notice] 4644#0: signal process started
    2015/01/04 21:08:07 [notice] 7135#0: signal process started
    2015/01/11 20:59:24 [notice] 5201#0: signal process started

    3. 打印当天22:13:53以后的日志

    shell> gawk -F ' ' '{if($1=="2015/01/03"&&($2>"22:13:53")){print $0}}' /usr/local/nginx/logs/error.log 
    
    2015/01/03 22:15:54 [notice] 31090#0: signal process started
    2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    2015/01/03 22:19:51 [notice] 32601#0: signal process started
    2015/01/03 22:19:51 [error] 32601#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
    2015/01/03 23:01:51 [notice] 4960#0: signal process started

    4. 打印5分钟内的日志

    shell> gawk -F ' ' '{if($1=="2015/01/03"&&($2>"22:13:53"&&$2<"22:18:53")){print $0}}' /usr/local/nginx/logs/error.log 
    2015/01/03 22:15:54 [notice] 31090#0: signal process started
    2015/01/03 22:15:54 [error] 31090#0: open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

    5. 每天定时切割nginx日志脚本

    #!/bin/bash
    # This script run at 00:00
    
    # The Nginx logs path
    logs_path="/usr/local/webserver/nginx/logs/"
    
    mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
    mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
    kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
    
    设置crontab,每天凌晨00:00切割nginx访问日志
    crontab -e
    
    输入以下内容:
    引用
    00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
  • 相关阅读:
    VB已死?还是会在Roslyn之下焕发新生?
    GitHub在Visual Studio 2015中获得TFS/VSO同等地位
    单体应用与微服务优缺点辨析
    对于JavaScript的函数.NET开发人员应该知道的11件事
    TypeScript 1.5 Beta带来修饰元数据支持
    Visual Studio 2015 RC中的ASP.NET新特性和问题修正
    Visual Studio从此走入非Windows程序猿家
    Azure DocumentDB对比MongoDB
    正确理解DTO、值对象和POCO
    大数据技术之_19_Spark学习_05_Spark GraphX 应用解析小结
  • 原文地址:https://www.cnblogs.com/phpfans/p/4264895.html
Copyright © 2011-2022 走看看