Centos/Linux 将 logrotate 设置为所有日志的最大文件大小 [英] Centos/Linux setting logrotate to maximum file size for all logs

查看:67
本文介绍了Centos/Linux 将 logrotate 设置为所有日志的最大文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

we use logrotate and it runs daily ... now we have had some situations where logs have grown significantly (read: gigbaytes) and killing our server. So now we would like to set a maximum filesize to the logs ....

我可以将它添加到 logrotate.conf 中吗?

can I just add this to the logrotate.conf?

大小 50M

然后它会应用于所有日志文件吗?还是我需要在每个日志的基础上进行设置?

and would it then apply to all log files? Or do I need to set this on a per log basis?

或者有什么其他建议?

(ps.我知道如果您想收到通知,日志会像描述的那样增长,而我们想要做的并不理想 - 但总比因为没有可用空间而无法再登录要好)

(ps. I understand that if you want to be notified is the log grows like described and what we want to do is not ideal - but it is better than not being able to logon anymore because there is no space available)

谢谢,肖恩

推荐答案

指定日志文件的大小来触发轮换.例如,一旦文件大小达到 50MB 或更大,size 50M 将触发日志轮换.您可以使用后缀 M 表示兆字节,使用 k 表示千字节,使用 G 表示千兆字节.如果不使用后缀,则表示字节.您可以在最后查看示例.有三个可用的指令 sizemaxsizeminsize.根据manpage:

It specifies the size of the log file to trigger rotation. For example size 50M will trigger a log rotation once the file is 50MB or greater in size. You can use the suffix M for megabytes, k for kilobytes, and G for gigabytes. If no suffix is used, it will take it to mean bytes. You can check the example at the end. There are three directives available size, maxsize, and minsize. According to 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 两个文件的解释.只要它们的大小超过 100k,它们就会被轮换,并且旧的日志文件在经过 5 次轮换后被邮寄(未压缩)到 www@my.org,而不是被删除.sharedscripts 意味着 postrotate 脚本只会运行一次(在旧日志被压缩之后),而不是为每个轮换的日志运行一次.请注意,本节开头第一个文件名周围的双引号允许 logrotate 旋转名称中带有空格的日志.应用正常的 shell 引用规则,支持 字符.

Here is an explanation for both files /var/log/httpd/access.log and /var/log/httpd/error.log. They are rotated whenever it grows over 100k in size, and the old logs files are mailed (uncompressed) to www@my.org after going through 5 rotations, rather than being removed. The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated. Note that the double quotes around the first filename at the beginning of this section allows logrotate to rotate logs with spaces in the name. Normal shell quoting rules apply, with ,, and characters supported.

这篇关于Centos/Linux 将 logrotate 设置为所有日志的最大文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆