zoukankan      html  css  js  c++  java
  • 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

    魅力男神
  • 相关阅读:
    Offer快到碗里来,囊中之物-CAS
    SQL 两表一对多关联,主表某字段保存所有关联的id
    No Feign Client for loadBalancing defined
    Clean Code读书笔记 3--类
    Clean Code读书笔记(2)---函数
    Clean Code读书笔记(1)---有意义的命名
    [CF1354D] Multiset
    [CF1365E] Maximum Subsequence Value
    [CF1358D] The Best Vacation
    [CF463C] Gargari and Bishops
  • 原文地址:https://www.cnblogs.com/capable/p/12171213.html
Copyright © 2011-2022 走看看