• Nginx日志切割及其各种服务日志随便切


    # 全服a/nginx日志切割全集
    如下是运用logrotate工具切割**a端日志**

    ```
    vim /etc/logrotate.conf/channelHandle
    /game/publish/pm2/*.log
    {
    su root root
    daily
    missingok
    copytruncate
    notifempty
    dateext
    sharedscripts
    postrotate
    /opt/move_log.sh
    endscript
    }

    ```

    #!/bin/bash
    datestamp=$(date +%Y%m%d)
    logdir=/game/log/$datestamp
    mkdir -p $logdir
    mv /game/publish/pm2/*log-$datestamp $logdir
    find /game/log -mindepth 1 -maxdepth 1 -mtime +60
    -type d | xargs rm -rf
    ```
    ```

    chmod +x /opt/move_log.sh

    如下更改3-22点的时间即可
    [root@logstash ash]# cat /etc/anacrontab
    # /etc/anacrontab: configuration file for anacron

    # See anacron(8) and anacrontab(5) for details.

    SHELL=/bin/sh
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    # the maximal random delay added to the base delay of the jobs
    RANDOM_DELAY=45
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=3-22

    #period in days delay in minutes job-identifier command
    1 5 cron.daily nice run-parts /etc/cron.daily
    7 25 cron.weekly nice run-parts /etc/cron.weekly
    @monthly 45 cron.monthly nice run-parts /etc/cron.monthly

    ```

    如下是切割**nginx日志要点**
    重点是postrotate
    ```
    [root@logstash ash]# cat /etc/logrotate.d/nginx
    /usr/local/nginx/logs/*.log
    {
    su root root
    daily
    missingok
    copytruncate
    notifempty
    dateext
    sharedscripts
    postrotate
    /opt/scripts/move_log.sh
    /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
    }
    ```
    如下脚本内容不变,都是生成当前时间,输出到新的目录作为旧日志文件存放位置
    ```
    [root@logstash ash]# cat /opt/move_log.sh
    #!/bin/bash
    datestamp=$(date +%Y%m%d)
    logdir=/tmp/$datestamp
    mkdir -p $logdir
    mv /usr/local/nginx/logs/*.log-$datestamp $logdir
    find /tmp -mindepth 1 -maxdepth 1 -mtime +60
    -type d | xargs rm -rf
    ```

    logrotate -vf /etc/logrotate.d/nginx

    魅力男神
  • 相关阅读:
    【译】Using .NET for Apache Spark to Analyze Log Data
    边缘缓存模式(Cache-Aside Pattern)
    GUID做主键真的合适吗
    在Java大环境下.NET程序员如何夺得一线生机
    板子
    P1525 关押罪犯 (并查集 / 二分图)| 二分图伪码
    算法学习笔记:匈牙利算法
    POJ
    19级暑假第三场训练赛
    POJ 1011 Sticks​ (DFS + 剪枝)
  • 原文地址:https://www.cnblogs.com/capable/p/12171213.html
Copyright © 2020-2023  润新知