在进程仍在运行时旋转日志文件


16

我正在运行一个将标准输出和标准错误写入日志文件的进程/var/log/dragonturtle.log。无论如何,有没有旋转日志文件的过程,并且该进程可以继续写入新的日志文件而不会终止该进程?

当前发生了什么(鉴于下面的logrotate配置):

  • 处理写入 /var/log/dragonturtle.log
  • logrotate的移动/var/log/dragonturtle.log/var/log/dragonturtle.log.1
  • 流程继续写入 /var/log/dragonturtle.log.1

我想发生的事情:

  • 处理写入 /var/log/dragonturtle.log
  • 将副本/var/log/dragonturtle.log对数旋转/var/log/dragonturtle.log.1
  • Logrotate截断 /var/log/dragonturtle.log
  • 流程继续写入 /var/log/dragonturtle.log

/etc/logrotate.d/dragonturtle

/var/log/dragonturtle.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 644 dragonturtle dragonturtle
}

Answers:


14

logrotate您所描述的选项是copytruncate。只需将此选项添加到您现有的logrotate配置中即可。这是logrotate.conf手册的摘录:

   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 can not 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  log-
          ging  data  might be lost.  When this option is used, the create
          option will have no effect, as the old log file stays in  place.

我打算提出一些更复杂的方法,包括索引节点检查和分叉的过程,但这更简单。
高度
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.