Logrotate 附加到现有文件并截断​​原始文件

Logrotate 附加到现有文件并截断​​原始文件

是否可以使用 logrotate 获取日志内容并将其添加到现有文件中?

像这样:
1. 将 /var/log/vnc.log 的内容放在 /archive/logs/vnc.log 的内容之后
2. 截断 /var/log/vnc.log

在 bash 中它将像这样:
1. cat /var/log/vnc.log >> /archive/logs/vnc.log
2. echo > /var/log/vnc.log

我认为这是不可能的。那么,逻辑方法是什么?在 cronjob 中执行上述命令?还是这个设置太脏了?

答案1

来自 logrotate 手册页:

 prerotate/endscript
          The  lines  between  prerotate and endscript (both of which must
          appear on lines by  themselves)  are  executed  (using  /bin/sh)
          before the log file is rotated and only if the log will actually
          be rotated. These directives may only appear inside a  log  file
          definition.  Normally,  the  absolute  path  to  the log file is
          passed as first argument to the script.   If   sharedscripts  is
          specified,  whole  pattern  is  passed  to the script.  See also
          postrotate.  See sharedscripts  and  nosharedscripts  for  error
          handling.

因此,您应该在 logrotate.conf 中添加如下条目:

/var/log/vnc.log 
{
  rotate 6
  monthly
  compress
  missingok
  prerotate
      cat /var/log/vnc.log >> /archive/logs/vnc.log
  endscript
}

相关内容