Centos/Linux将logrotate设置为所有日志的最大文件大小

vh0rcniy  于 2022-11-07  发布在  Linux
关注(0)|答案(3)|浏览(817)

我们使用logrotate并且它每天运行...现在我们遇到了一些日志显著增长的情况(请阅读:所以现在我们要设置日志的最大文件大小....
我能把这个添加到logrotate.conf文件中吗?
尺寸50 M
然后它会应用于所有日志文件吗?或者我需要在每个日志的基础上设置它吗?
或者其他建议?
(ps.我知道,如果您希望收到通知,则日志会像所描述的那样增长,而我们想要做的事情并不理想-但这总比因为没有可用空间而无法登录要好)
谢谢,肖恩

af7jpaap

af7jpaap1#

正如Zeeshan所提到的,logrotate选项sizeminsizemaxsize是旋转的触发器。
为了更好地解释它。你可以经常运行logrotate,但是除非达到了一个阈值,比如达到了文件大小或者经过了适当的时间,否则日志将不会被轮转。
大小选项不能确保循环日志也具有指定的大小。要使它们接近指定的大小,您需要足够频繁地调用logrotate程序。这一点很重要。

对于快速生成的日志文件(例如,每天数百MB),除非您希望它们非常大,否则您需要确保经常调用logrotate!这一点非常重要。

因此,为了防止磁盘被数GB的日志文件填满,您需要确保足够频繁地调用logrotate,否则日志轮转将无法像您希望的那样工作。
在Ubuntu上,通过将脚本/etc/cron.daily/logrotate移动到/etc/cron.hourly/logrotate,可以轻松地切换到每小时轮换
或添加


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

到你的/etc/crontab文件。每5分钟运行一次。
size选项忽略了daily,weekly,monthly时间选项。但是minsize和maxsize会将其考虑在内。
手册页有点混乱。下面是我的解释。

***minsize仅在文件达到适当大小且超过设置的时间段后才会循环。***例如,最小大小50 MB+每日如果文件在每日时间结束前达到50 MB,则会一直增长到第二天。
***maxsize将在日志达到设定大小或经过适当时间后进行轮转。***例如,maxsize 50 MB + daily。如果文件大小为50 MB,并且我们还没有到达第二天,则日志将进行轮转。如果文件只有20 MB,并且我们将滚动到第二天,则文件将进行轮转。
***size将在日志〉size时进行轮转。无论是否指定了hourly/daily/weekly/monthly。***因此,如果您的大小为100 M-这意味着当您的日志文件〉100 M时,如果在此条件为真时运行logrotate,则日志将进行轮转。一旦轮转,主日志将为0,并且后续运行将不执行任何操作。

因此,在操作的情况下。具体来说,最大50 MB,我将使用如下内容:

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

这意味着他最多可以创建8个小时的日志。其中有8个日志,每个日志的大小不超过50 MB。由于他说他每天会得到数GB的日志,并且假设这些日志以相当恒定的速度增长,并且使用了maxsize,因此他最终得到的每个文件的大小将接近最大值。因此,每个文件的大小可能接近50 MB。考虑到这些日志所构建的卷,他将需要确保logrotate足够频繁地运行以满足目标大小。
由于我在这里设置了hourly,我们需要至少每小时运行一次logrotate。但是由于它们每天累积2GB,而我们需要50 MB......假设恒定速率为每小时83 MB。所以你可以想象,如果我们每小时运行一次logrotate,尽管将maxsize设置为50,但在这种情况下,我们最终会得到83 MB的日志。因此,在这种情况下,将运行时间设置为每20分钟或更短时间就足够了。
确保每20分钟运行一次logrotate。在这种情况下,在20分钟内日志文件可能小于50 MB,但在40分钟内,超过50 MB的日志文件将自动更新。如果我们每30分钟更新一次,则在30分钟内日志文件将小于50 MB(无旋转),在1小时内,它可能已经建立到〉80 MB,在这种情况下,文件将被旋转,但比我们预期的要大得多。因此,请确保将旋转时间考虑在内。


* /20 * * * * /etc/cron.daily/logrotate
jhdbpxl9

jhdbpxl92#

它指定要触发自动重建的**日志档 * a大小。例如,一旦档案大小为50MB或更大,size 50M就会触发日志自动重建。您可以使用后缀M代表MB,k代表KB,而G代表GB。如果未使用后缀,它将表示字节。您可以查看最后的示例。有三个可用指令sizemaxsizeminsize。根据manpage

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 www@my.org
           size 100k
           sharedscripts
           postrotate
               /usr/bin/killall -HUP httpd
           endscript
       }

下面是对文件/var/log/httpd/access.log/var/log/httpd/error.log的解释。(未压缩)转换为www@my.org,而不是删除。sharedscripts表示postrotate脚本将只运行一次(在压缩旧日志后),而不是对每个轮转的日志引用一次。请注意,此部分开头的第一个文件名两边的双引号允许logrotate轮转名称中带有空格的日志。适用普通的shell引用规则,支持,\字符。

j9per5c4

j9per5c43#

为了进一步简化说明:
Logrotate大小参数仅在运行logrotate时应用。
例如,如果你设置logrotate每小时运行一次,当文件大小达到5 MB时,如果文件在一个小时之前就超过了5 MB,那么文件实际上会增长到大于5 MB,因为logrotate从来没有在文件上调用过。
必须经常在文件上运行logrotate以检查其大小。因此,当在logrotate中使用size参数时,只需让logrotate的计时由其他东西来处理即可。(例如cron/script)。这意味着您可以在logrotate配置中省略指定时间。
例如,如果我想旋转一个大小为5 MB的文件-该文件达到该大小的速度将决定logrotate应该运行的频率。假设平均需要10分钟才能达到5 MB,首先,旋转设置最低为:

/var/log/path/the.log {
           rotate 1 (#number of rotations)
           size 5M
       }

创建目录/etc/custom-rotate.d
将上述内容保存在/etc/custom-rotate.d/customlog
使用权限:sudo chmod 644 /etc/custom-rotate.d/customlog
创建配置文件:

cat << EOF | sudo tee /etc/custom-rotate.conf

# packages drop custom log rotation information into this directory

include /etc/custom-rotate.d
EOF

权限:sudo chmod 644 /etc/custom-rotate.conf
然后每隔5分钟运行一次cron(为可能的异常留出空间)来检查大小。
第一个
新增项目:


* /5  *  *  *  * /usr/sbin/logrotate /etc/custom-rotate.d/customlog

重新加载cron:

sudo service cron reload

因此,logrotate将每5分钟运行一次,如果大小大于5 M,它将旋转日志。

相关问题