Linux

持續監控帶有偶爾旋轉的尾部的日誌

  • September 25, 2017

我們正在使用 tail 連續監控多個日誌,但是當日誌旋轉時,該文件的尾部將停止。

據我了解,問題在於當日誌旋轉時,會創建一個新文件,並且正在執行的尾部程序對該新文件句柄一無所知。

啊,這有一面旗幟。

而不是使用tail -f /var/log/file我們應該使用tail -F /var/log/file


tail -F翻譯為tail --follow=name --retry如;

  • --follow=name: 跟隨文件名而不是文件描述符
  • --retry:如果文件不可訪問,請稍後再試,而不是死掉
# tail --follow=mylog.log

人尾

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.

所以在這種情況下使用該-F選項是正確的。

-F     same as --follow=name --retry

引用自:https://serverfault.com/questions/53699