您如何将 logrotate 与输出重定向一起使用?

How do you use logrotate with output redirect?

我目前 运行 一个 ruby 脚本,它将其 HTTP 流量记录到标准输出。因为我希望日志持久化,所以我将输出重定向到带有 ruby ruby_script.rb >> /var/log/ruby_script.log 的日志文件。但是,日志现在变得非常大,所以我想使用以下方法实现 logrotate:

"/var/log/ruby_script.log" {
  missingok
  daily
  rotate 10
  dateext
}

但是,在 运行 logrotate --force -v ruby_script 之后 "ruby_script" 是 logrotate.d 配置文件的名称,没有创建新文件供脚本写入,并且它改为写入旋转文件。我猜这种行为的发生是因为 >> 传递的文件描述符无论移动它都会粘在文件上,并且在第一次调用后与文件名无关。因此,我的问题是,实现我正在寻找的功能的正确方法是什么?

查看选项 copytruncate

来自man logrotate

copytruncate: Truncate the original log file to zero size 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.