我有一个名为php.log的8GB文件,并运行一个正在运行的php脚本。 logging每个事件对我来说很重要,我想压缩它并清空当前文件而不停止Web服务器。
如果我跑
mv php.log php.log.backup20140305-01 touch php.log
我会失去一些数据。 我怎么能做到这一点,而不会丢失任何数据?
你会发现更容易configurationlogrotate为你做旋转。 如果您创build一个名为/etc/logrotate.d/php的文件,它包含如下内容,它将自动处理日志轮转。 这只是一个指导,所以在投入生产之前一定要testing和定制它。
/path/to/php.log { daily missingok # don't rotate if the file isn't there... notifempty # ...or if it's zero-length rotate 30 # keep 30 days' worth of logs compress # gzip the logs, but... delaycompress # ...only after they're over a day old create 640 root adm # permissions with which to create new files sharedscripts postrotate /etc/init.d/apache2 graceful # or whatever makes your process let go of the log file endscript }
注意:这个提取中的注释会破坏logrotate语法,所以一定要将它们从实际configuration文件中删除。
你应该像Flup所说的那样使用logrotate,但是在不破坏当前文件句柄的情况下,简化/截断日志文件的一种快速方法是:
> /path/to/php.log
请注意,这样做会损失数据 ,所以如果您确定自己的数据是其他地方的数据,则应该截断,但是如果您不关心日志文件的内容,一个不间断的写作过程。