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

    魅力男神
  • 相关阅读:
    LNMP搭建随笔
    MySQL中concat函数(连接字符串)
    解决bash: mysql: command not found 的方法
    linux下导入、导出mysql数据库命令
    MysqL的root用户不允许远程连接,只能通过PHPMYADMIN
    CentOS7安装iptables防火墙
    为Linux服务器设置静态IP的方法
    linux下mysql的root密码忘记解决方法
    tcp的三次握手和四次挥手转自https://www.jianshu.com/p/d3725391af59
    go if for while 的使用
  • 原文地址:https://www.cnblogs.com/capable/p/12171213.html
Copyright © 2011-2022 走看看