有没有办法告诉 logrotate 忽略打开的文件?

有没有办法告诉 logrotate 忽略打开的文件?

我搜索了一下,找不到任何东西。我不想logrotate触摸当前打开的文件。我正在考虑做一些事情,lsof但我没有找到让任何脚本中止操作的方法。有任何想法吗?

答案1

如果您已nosharedscripts设置(默认设置),并且prerotate脚本因错误退出,则受影响的日志文件将不会对其采取任何进一步的操作*。

所以,理论上,你可能会得到类似的东西(警告,未经测试):

/var/log/application.log {
    nosharedscripts
    prerotate
        logfile=$1
        lsof $logfile >/dev/null
        exit $?
    endscript
    ...
}

因此,如果lsof找不到任何$logfile打开的进程,prerotate脚本将退出1logrotate不会对该日志执行任何操作。


*来自logrotate(8)linux.die.net 上的手册页:

nosharedscripts
Run prerotate and postrotate scripts for every log file which is rotated
(this is the default, and overrides the sharedscripts option). The absolute
path to the log file is passed as first argument to the script. If the
scripts exit with error, the remaining actions will not be executed for the
affected log only.
...
prerotate/endscript
The lines between prerotate and endscript (both of which must appear on
lines by themselves) are executed (using /bin/sh) before the log file is
rotated and only if the log will actually be rotated. These directives may
only appear inside a log file definition. Normally, the absolute path to
the log file is passed as first argument to the script. If sharedscripts is
specified, whole pattern is passed to the script. See also postrotate. See
sharedscripts and nosharedscripts for error handling.

可能取决于您的logrotate版本,但我找不到任何关于哪些版本这样做/不这样做的文档。

答案2

如果应用程序将文件移动app.log到您可以这样app.log.old使用:logrotate

/path/to/app/logs/*.old {
    missingok
    nocreate
    nocopytruncate
    nodelaycompress
    notifempty

     ... 
}

这只会logrotate处理应用程序已完成的那些文件,因此不会打开。

答案3

你可以使用这样的东西:

/path/*.log
{
  copytruncate
  missingok
  notifempty
...

相关内容