旋转一直打开并正在写入的文件

我有一个Linux应用程序不断写入日志信息到日志文件,例如。 /var/log/application.log 。 由于应用程序不会自动旋转文件,因此该日志文件可能在几周内达到千兆字节的大小,所以我希望能够正确地旋转此文件

我主要关心的是要旋转应用程序随时打开的文件,我可能需要:

  1. 将文件移动到其旋转的forms/var/log/application.log -> /var/log/application.log.2013-01-28

  2. 创build一个空的/var/log/application.logObs:此时应用程序仍在写入/var/log/application.log.2013-01-28

  3. 更改应用程序进程的文件描述符以再次指向/var/log/application.log

那么,对吗? 如果是这样,我该怎么做? (主要是改变文件描述符部分)

如果我不是,那么正确的方法是什么?如何去做?

写一个logrotateconfiguration来使用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. 

大多数这样的应用程序都会响应SIGHUP等信号,并在收到信号后closures并重新打开日志文件。 检查您的应用程序的文档以find要发送的正确信号。