Linux上的自由空间驱动的日志轮换?

有人问我“我们的应用程序应该保留多久”,我的回答是“直到磁盘满了”,因为除了空间不足之外,没有理由把它们扔掉。

然而,标准的logrotate希望我们指定一个特定的周期+旋转次数。 有没有类似的东西让我们说:“每天轮换,并保持尽可能多的历史,直到只有5%的空间自由”?

该平台是Redhat Linux。

您也许可以使用第一个或lastaction指令来调用testing磁盘空间的shell脚本,然后对最旧的文件运行删除。

firstaction/endscript The lines between firstaction and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh) once before all log files that match the wildcarded pattern are rotated, before prerotate script is run and only if at least one log will actually be rotated. These directives may only appear inside a log file definition. Whole pattern is passed to the script as first argument. If the script exits with error, no further processing is done. See also lastac- tion. 

更新:

这里是一个关于您可以运行的脚本types的Stackoverflow文章:

https://stackoverflow.com/questions/7523059/remove-oldest-file-in-repository

logrotate本身没有这个选项。 您可以添加一个cron脚本,查找可用空间低于条件时要删除的最旧日志。 你也可以做一些其他的validation。 但是,始终让磁盘太满并不是一个好主意,因为系统将无法创build大量的临时文件,并可能导致应用程序失败。

我只想指出,有些情况下您不希望日志填满所有可用的磁盘空间。 我已经处理了使用自动精简configuration/ var目录的多台主机,并将日志保持在一定的大小是至关重要的。 我们用一个亲信的工作与logrorate结合起来,以保持体积不变。 类似的东西可以在你的环境中使用,尽pipe像splunk或syslog-ng这样的中央日志服务器可能是更好的select。