持续监控带有偶尔轮换的尾部的日志

xkc*_*150 22 linux monitoring log-files tail logrotate

我们使用 tail 来连续监视多个日志,但是当日志旋转时,该文件的尾部将停止。

据我了解,问题是当日志轮换时,会创建一个新文件,而正在运行的尾部进程对该新文件句柄一无所知。

xkc*_*150 36

啊,这有一面旗帜。

tail -f /var/log/file我们应该使用而不是使用tail -F /var/log/file


tail -F翻译tail --follow=name --retry成在;

  • --follow=name: 跟随文件名而不是文件描述符
  • --retry: 如果文件无法访问,请稍后重试而不是死亡


djh*_*ell 27

# tail --follow=mylog.log
Run Code Online (Sandbox Code Playgroud)

人尾巴

With --follow (-f), tail defaults to  following  the  file  descriptor,
       which  means that even if a tail’ed file is renamed, tail will continue
       to track its end.  This default behavior  is  not  desirable  when  you
       really want to track the actual name of the file, not the file descrip?
       tor (e.g., log rotation).  Use --follow=name in that case.  That causes
       tail  to track the named file by reopening it periodically to see if it
       has been removed and recreated by some other program.
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下使用该-F选项是正确的。

-F     same as --follow=name --retry
Run Code Online (Sandbox Code Playgroud)


Jim*_*ski 11

确切的答案取决于您的操作系统 - 但在许多情况下,tail -F会做正确的事情。

  • 如果 tail -F 不起作用,请编译一个可以运行的 tail -F 版本。另一种选择是通往疯狂小镇的短途道路。 (3认同)

ret*_*ile 5

tail -F 或 tail --follow=name