• 常用日志维护脚本


    日志格式

    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
  • 相关阅读:
    json
    [题解]luogu_P2151_HH去散步(矩阵floyd
    [题解]数字计数(数位dp(模板向
    【简单计数知识】JZOJ6395. 【NOIP2019模拟2019.10.28】消失的序列
    字符云例子
    JAVA FileUtils(文件读写以及操作工具类)
    AT2657 Mole and Abandoned Mine
    Problem: [Usaco2018 Open]Team Tic Tac Toe
    Problem: [USACO2018 Jan]Blocked Billboard II
    算法——星星树
  • 原文地址:https://www.cnblogs.com/phpfans/p/4264895.html
Copyright © 2020-2023  润新知