zoukankan      html  css  js  c++  java
  • 用Linux自带的Logrotate来管理日志

    Logrotate是由cron控制,cron在规定的时间执行 " logrotate  /etc/logrotate.conf "命令。将对象日志进行转储,删除,压缩等操作。。。

    这是logrotate日志轮替工具的一段官方简介:

    The logrotate utility is designed to simplify the administration of log files on a system which generates a lot of log files. Logrotate allows for the automatic rotation compression, removal and mailing of log files. Logrotate can be set to handle a log file daily, weekly, monthly or when the log file gets to a certain size.

    为了使用它,主要有两个地方需要修改一下:一个是/etc/logrotate.conf,另一个是/etc/logrotate.d/下面的文件。

    你既可以在logrotate.conf中直接定义如何处理你的log文件,也可以在/logrotate.d/下面针对自己的log新建一个对应的文件来定义处理log的行为。

    这里是logrotate命令的详细解释的链接:http://linuxcommand.org/man_pages/logrotate8.html

    下面是一个对Nginx日志进行定时切割压缩的例子:

    # cat  /etc/logrotate.d/nginx 

    复制代码
    /data/logs/nginx/*.log {
        rotate 365
        copytruncate
        create
        missingok
        notifempty
        sharedscripts
        compress
        postrotate
        [ -f /var/run/nginx.pid  ] && kill -USR1 `cat /var/run/nginx.pid`
        endscript
    }
    复制代码

    monthly:说明是一个月进行一次处理,常用的还有daily,weekly

    rotate 365:意思是说轮替时最多保留365个备份,多余的老的日志就被覆盖了。最新生成的始终为 *.1.gz ,依次往后推和进行覆盖

    copytruncate: 的作用在于先复制一份当前日志文件用做处理,再清空源日志文件,让其继续接收日志。(清空但不删除日志文件)

    create:处理完该日志文件后,新生成一个日志文件,当然尽可能是同名同权限等

    olddir:定义了旧的日志存储在哪里

    missingok:意思是如果上述*.log文件找不到的话也不报错,直接跳过

    notifempty: 如果日志文件为空的话,则不进行rotate轮替操作

    sharedscripts:和endscript对应,中间放脚本

    prerotate: 在启动logrotate之前执行的命令或脚本,例如修改文件或目录的属性等

    postrotate:在启动logrotate之后执行的命令或脚本,例如使某项服务重新载入配置文件,重新打开日志文件 (kill -HUP或 kill -USR1)

    这里多说一句,kill -HUP与kill -USR1的区别仅在于kill -HUP会对进程进行复位操作,相当于/usr/local/nginx/sbin/nginx -s reload,而kill -USR1执行后主子进程号均不变

    nocompress:说明旧日志不需要被压缩保存

    minsize 1G: 日志最小1G,不到1G不轮替

    logrotate定义了如何处理日志,而它本身则是被crond定时调用的。crond是一个Unix系操作系统中的定时调度软件,下面一段文字是从wiki上抄来的:

    The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.

    默认的logrotate是一天运行一次,它的脚本被放在/etc/cron.daily/下面。除了cron.daily外还有cron.weekly,cron.monthly,cron.hourly等分别对应不同的频率,你可以根据自己的需要把脚本放在不同的文件夹下面。在设置外所有东西以后,别忘了使用chkconfig crond on来保证它会一直开机运行。然后就大工告成了。

    # cat /etc/cron.daily/logrotate (默认的) 

    复制代码
    #!/bin/sh
    
    /usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
    EXITVALUE=$?
    if [ $EXITVALUE != 0 ]; then
        /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
    fi
    exit 0
    复制代码

    如果想自定义执行时间的频率的话,自定义计划任务即可,例如下面:(每个整点执行一次)

    0 * * * *  root  /usr/sbin/logrotate  -f  /root/nginx              -f, --force        Force file rotation

    查看各log文件rotate的具体执行情况:

    cat  /var/lib/logrotate.status

     
     
     
    参考资料:http://www.cnblogs.com/tiantiandas/p/rsyslog.html
                  http://www.cnblogs.com/futeng/p/4785206.html
  • 相关阅读:
    java修饰符 protect public protected
    java中interface使用
    java中super的用法
    引用的一道JAVA题目
    java中==和equals的区别(转)
    2019PHP面试题最全面归纳总结
    (一)PHP基础知识考察点
    Linux常用命令大全(非常全!!!)
    MAMP mysql无法启动 总结(以后有发现再添加)
    win 安装composer (详细教程)
  • 原文地址:https://www.cnblogs.com/wajika/p/6426239.html
Copyright © 2011-2022 走看看