Linux
旋轉一直打開且正在寫入的文件
我有一個 linux 應用程序,它不斷將日誌資訊寫入日誌文件,例如。
/var/log/application.log
. 由於應用程序不會自動旋轉文件,這個日誌文件可能會在幾週內達到千兆字節的大小,所以我希望能夠正確旋轉這個文件我主要關心的是,要旋轉始終由應用程序打開的文件,我可能需要:
- 將文件移動到其旋轉形式
/var/log/application.log -> /var/log/application.log.2013-01-28
- 創建一個空的
/var/log/application.log
. *Obs:*此時應用程序仍在寫入/var/log/application.log.2013-01-28
- 更改應用程序程序的文件描述符以再次指向
/var/log/application.log
那麼,我說的對嗎?如果是這樣,我該怎麼做?(主要是改變文件描述符部分)
如果我不是,正確的方法是什麼以及如何做?
編寫
logrotate
要使用的配置copytruncate
copytruncate Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.