2013-11-23 68 views
31

我们使用logrotate并且它每天都在运行......现在我们遇到了日志显着增长(读取:gigbaytes)和杀死我们的服务器的情况。所以现在我们想设置一个最大文件大小的日志....Centos/Linux将logrotate设置为所有日志的最大文件大小

我可以只添加到logrotate.conf?

大小50M

,并将它然后适用于所有的日志文件?或者我需要在每个日志的基础上进行设置?

或其他建议? (ps。据我所知,如果你想得到通知,日志会像描述的一样增长,我们想要做的事情并不理想,但是由于没有空间可用,所以它比不能登录更好)

感谢,肖恩

回答

37

它指定日志文件的大小来触发旋转。例如,一旦文件大小为50MB或更大,size 50M将触发日志轮转。您可以使用后缀M兆字节,k(千字节)和G(千兆字节)。如果没有使用后缀,它将意味着字节。你可以在最后查看例子。有三个指令可用size,maxsizeminsize。根据手册页:

minsize size 
       Log files are rotated when they grow bigger than size bytes, 
       but not before the additionally specified time interval (daily, 
       weekly, monthly, or yearly). The related size option is simi- 
       lar except that it is mutually exclusive with the time interval 
       options, and it causes log files to be rotated without regard 
       for the last rotation time. When minsize is used, both the 
       size and timestamp of a log file are considered. 

size size 
       Log files are rotated only if they grow bigger then size bytes. 
       If size is followed by k, the size is assumed to be in kilo- 
       bytes. If the M is used, the size is in megabytes, and if G is 
       used, the size is in gigabytes. So size 100, size 100k, size 
       100M and size 100G are all valid. 
maxsize size 
       Log files are rotated when they grow bigger than size bytes even before 
       the additionally specified time interval (daily, weekly, monthly, 
       or yearly). The related size option is similar except that it 
       is mutually exclusive with the time interval options, and it causes 
       log files to be rotated without regard for the last rotation time. 
       When maxsize is used, both the size and timestamp of a log file are     
       considered. 

下面是一个例子:

"/var/log/httpd/access.log" /var/log/httpd/error.log { 
      rotate 5 
      mail [email protected] 
      size 100k 
      sharedscripts 
      postrotate 
       /usr/bin/killall -HUP httpd 
      endscript 
     } 

下面是这两个文件/var/log/httpd/access.log/var/log/httpd/error.log的解释。只要它的大小超过100k,它们就会被旋转,而旧的日志文件在经过5次旋转后被邮寄(未压缩)到[email protected],而不是被删除。 sharedscripts意味着postrotate脚本只会运行一次(在旧日志被压缩后),而不是每个被旋转的日志都运行一次。请注意,本节开头处的第一个文件名周围的双引号允许logrotate用名称中的空格旋转日志。正常shell引用规则适用,支持,\个字符。

+1

不错。一个小纸条,我发现maxsize和minsize在旧发行版中不能很好地工作(比如centos5和ubuntu 10.10 ...后者是EOL)。 – tobixen

0

我从来没有使用过它作为全局选项,但测试它的一种方法是使用-d标志。这不会处理任何旋转,但它会告诉你什么logrotate会做。

所以我刚刚用-d标志测试了这个。我的经历似乎违背了手册页。如果我设置了一个'大小10',如果日志文件大于10个字节,并且将旋转设置为每周,那么它将不会旋转文件,即使它大于10个字节。如果我注释掉“每周”参数,那么logrotate表示它将旋转文件。这似乎与我在手册页上所期望的相反。

+0

为什么?阅读@Zeeshan的手册页摘录我期待这种行为......(“minsize:...日志文件在长度大于字节大小时被旋转,但*不在额外指定的时间间隔之前......”) – MarcoS

49

正如我所提到的Zeeshan,logrotate选项size,minsize,maxsize是旋转的触发器。

为了更好地解释它。您可以随心所欲地运行logrotate,但除非达到阈值(例如正在达到的文件大小或经过适当的时间),否则日志将不会旋转。

大小选项不能确保您的旋转日志也是指定的大小。为了使它们接近指定的大小,你需要经常调用logrotate程序。这很关键。

对于构建速度非常快(例如每天数百MB)的日志文件,除非您希望它们非常大,否则需要确保经常调用logrotate!这很关键。

因此,要停止使用多千兆字节日志文件填充磁盘,您需要确保足够频繁地调用logrotate,否则日志轮换将无法正常工作。

在Ubuntu,你可以方便地切换到每小时旋转通过移动脚本/etc/cron.daily/logrotate到/etc/cron.hourly/logrotate

或者添加

*/5 * * * * /etc/cron.daily/logrotate 

要将/ etc/crontab文件。每5分钟运行一次。

size选项忽略每日,每周,每月的时间选项。但最大尺寸&考虑到它。

手册页有点混淆。这是我的解释。

minsize仅当文件达到适当的大小并且经过了设定的时间段时才旋转。例如每日最大容量为50MB + 如果文件在日常时间点击之前达到50MB,则会在第二天持续增长。

maxsize将在日志达到设定大小或经过适当的时间时进行旋转。 例如最大限度每天50MB +。 如果文件是50MB,我们还没有在第二天,日志将被旋转。如果文件只有20MB,并且我们转到第二天,那么文件将被旋转。

size当log> size时会旋转。无论是否指定小时/每日/每周/每月。因此,如果您的大小为100M - 这意味着当您的日志文件大于100M时,如果在此条件为真时运行logrotate,则日志将被旋转。旋转后,主日志将为0,随后的运行将不会执行任何操作。

所以在op的情况下。特别是50MB最多我会使用类似如下:

/var/log/logpath/*.log { 
    maxsize 50M 
    hourly 
    missingok 
    rotate 8 
    compress 
    notifempty 
    nocreate 
} 

这意味着他会创建8小时的日志最大。其中有8个每个不超过50MB。由于他说他每天得到几千兆字节,并且假设他们以相当恒定的速率建立,并且使用了最大化,他将以接近每个文件达到的最大值为结束。所以它们每个可能接近50MB。鉴于他们建立的数量,他需要确保logrotate的运行频率足以达到目标规模。

由于我已经把时间放在那里,我们需要logrotate每小时至少运行一次。但是,由于他们每天要创建2GB的数据,并且我们需要50MB ...假设每小时83MB的恒定速率。所以你可以想象,如果我们每小时运行一次logrotate,尽管将maxsize设置为50,那么在这种情况下,我们最终会得到83MB的日志。所以在这种情况下,每运行30分钟或更少时间就足够了。

确保logrotate每30分钟运行一次。

*/30 * * * * /etc/cron.daily/logrotate 
+0

您能否解释更多关于“尺寸选项忽略每日,每周,每月时间选项”的更多信息。 – Jisson

+0

它是否与maxsize几乎相同? – Jisson

+0

好的,添加大小的解释 – Matt

相关问题