• nginx切割日志


    1、nginx不停服务进行切割日志:

    [root@weblogic scripts]# cat nginx_log.sh 
    #!/bin/bash
    
    log_path=/var/log/nginx
    YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
    nginx_cmd="/usr/sbin/nginx"
    
    /bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
    /bin/mv ${log_path}/error.log  ${log_path}/error.$YESTERDAY.log
    
    # reopen a new log file
    ${nginx_cmd} -s reopen
    

     然后进行定时任务设定,设置在凌晨12点:

    [root@weblogic nginx]# crontab -l
    0 0 * * * /bin/bash /root/scripts/nginx_log.sh
    

     测试效果:

    [root@weblogic nginx]# ll
    总用量 8
    -rw-r--r-- 1 nginx root   0 12月 15 02:13 access.2017-12-14_01.log
    -rw-r--r-- 1 nginx root   0 12月 15 08:04 access.log
    -rw-r--r-- 1 nginx root   0 12月 15 03:09 error.2017-12-14_01.log
    -rw-r--r-- 1 nginx root  61 12月 15 08:04 error.log
    

     关于nginx命令中的几个信号处理,请查看官方文档:http://nginx.org/en/docs/beginners_guide.html

    2、使用kill命令向nginx主进程发送一个信号:

    #向nginx主进程发送USR1信号,重新打开日志文件,否则会继续往mv后的文件写数据的。原因在于:linux系统中,内核是根据文件描述符来找文件的。如果不这样操作导致日志切割失败。

    # kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`
    

     所以该切割脚本如下:

    [root@weblogic scripts]# cat nginx_sin.sh 
    #!/bin/bash
    
    log_path=/var/log/nginx
    YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S`
    nginx_cmd="/usr/sbin/nginx"
    
    /bin/mv ${log_path}/access.log ${log_path}/access.$YESTERDAY.log
    /bin/mv ${log_path}/error.log  ${log_path}/error.$YESTERDAY.log
    
    # send a signal
    /bin/kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`
    

     执行结果如下:

    [root@weblogic nginx]# ll
    总用量 8
    -rw-r--r-- 1 nginx root   0 12月 15 08:04 access.2017-12-14_49.log
    -rw-r--r-- 1 nginx root   0 12月 15 08:28 access.log
    -rw-r--r-- 1 nginx root  61 12月 15 08:04 error.2017-12-14_49.log
    -rw-r--r-- 1 nginx root   0 12月 15 08:28 error.log
    

     现在将脚本放进到crontab定时脚本中,凌晨12点执行:

    [root@weblogic nginx]# crontab -l
    0 0 * * * /bin/bash /root/scripts/nginx_sin.sh
    

    3、使用logrotate进行切割:

     1、安装:

    yum -y install logrotate

    2、配置logrotate:

    [root@weblogic logrotate.d]# pwd
    /etc/logrotate.d
    [root@weblogic logrotate.d]# cat nginx
    /var/log/nginx/*log {
        daily
        rotate 10
        dateext
        missingok
        notifempty
        # compress
        delaycompress
        create 640 nginx adm
        sharedscripts
        postrotate
            [ -f /var/run/nginx.pid ] && /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
        endscript
    }

    3、配置参数:

    /var/log/nginx/为nginx日志的存储目录,可以根据实际情况进行修改。

    daily:日志文件将按天轮循。

    weekly:日志文件将按周轮循。

    monthly:日志文件将按月轮循。

    missingok:在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。

    rotate 7:一次存储7个日志文件。对于第8个日志文件,时间最久的那个日志文件将被删除。

    dateext:定义日志文件后缀是日期格式,也就是切割后文件是:xxx.log-20160402.gz这样的格式。如果该参数被注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式。

    compress:在轮循任务完成后,已轮循的归档将使用gzip进行压缩。

    delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。

    notifempty:如果是空文件的话,不进行转储。

    create 640 nginx adm:以指定的权限和用书属性,创建全新的日志文件,同时logrotate也会重命名原始日志文件。

    postrotate/endscript:在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd进程将立即再次读取其配置并继续运行。注意:这两个关键字必须单独成行。

    4、查看logrotate切割日志的时间:

    [root@weblogic logrotate.d]# 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
    # the jobs will be started during the following hours only
    START_HOURS_RANGE=3-22
  • 相关阅读:
    设计模式-行为型模式,python备忘录模式
    设计模式-行为型模式,python 中介者模式
    python 迭代器模式
    python对象池模式
    设计模式-结构型模式,python组合模式
    设计模式-结构型模式,python桥接模式
    python concurrent.futures.Threadpoolexcutor的有界队列和无界队列
    python加快数据处理的方法
    面向切面编程AOP,一些通用装饰器
    supervisor的command执行两条命令
  • 原文地址:https://www.cnblogs.com/jsonhc/p/8120520.html
Copyright © 2020-2023  润新知