我有一个
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.