我们有一个使用 syslog 进行日志记录的自定义应用程序,现在我们面临 logrotate 的问题,日志正在写入 urls.log.1 而不是 urls.log ,logrotate 在旋转后不会创建 url.log。我们正在使用Ubuntu 14.04 LTS 服务器。我们如何强制 logrotate 在旋转后创建 urls.log ?
logrotate 配置(/etc/logrotate.d/rsyslog)
/usr/local/app/log/urls.log
{
rotate 10
daily
missingok
notifempty
compress
delaycompress
sharedscripts
su app app
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
}
如果我手动 mv urls.log.1 到 urls.log 并运行 force logrotate ,这将是输出
considering log /usr/local/app/log/urls.log
log needs rotating
rotating log /usr/local/app/log/urls.log, log->rotateCount is 10
dateext suffix '-20170228'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
switching uid to 1000 and gid to 1000
renaming /usr/local/app/log/urls.log.10.gz to /usr/local/app/log/urls.log.11.gz (rotatecount 10, logstart 1, i 10),
renaming /usr/local/app/log/urls.log.9.gz to /usr/local/app/log/urls.log.10.gz (rotatecount 10, logstart 1, i 9),
renaming /usr/local/app/log/urls.log.8.gz to /usr/local/app/log/urls.log.9.gz (rotatecount 10, logstart 1, i 8),
renaming /usr/local/app/log/urls.log.7.gz to /usr/local/app/log/urls.log.8.gz (rotatecount 10, logstart 1, i 7),
renaming /usr/local/app/log/urls.log.6.gz to /usr/local/app/log/urls.log.7.gz (rotatecount 10, logstart 1, i 6),
renaming /usr/local/app/log/urls.log.5.gz to /usr/local/app/log/urls.log.6.gz (rotatecount 10, logstart 1, i 5),
renaming /usr/local/app/log/urls.log.4.gz to /usr/local/app/log/urls.log.5.gz (rotatecount 10, logstart 1, i 4),
renaming /usr/local/app/log/urls.log.3.gz to /usr/local/app/log/urls.log.4.gz (rotatecount 10, logstart 1, i 3),
renaming /usr/local/app/log/urls.log.2.gz to /usr/local/app/log/urls.log.3.gz (rotatecount 10, logstart 1, i 2),
renaming /usr/local/app/log/urls.log.1.gz to /usr/local/app/log/urls.log.2.gz (rotatecount 10, logstart 1, i 1),
renaming /usr/local/app/log/urls.log.0.gz to /usr/local/app/log/urls.log.1.gz (rotatecount 10, logstart 1, i 0),
old log /usr/local/app/log/urls.log.0.gz does not exist
renaming /usr/local/app/log/urls.log to /usr/local/app/log/urls.log.1
running postrotate script
switching euid to 0 and egid to 0
removing old log /usr/local/app/log/urls.log.11.gz
switching euid to 0 and egid to 0
如果我再次强制运行 logrotate 这将是输出
rotating pattern: /usr/local/app/log/urls.log
forced from command line (100 rotations)
empty log files are not rotated, old logs are removed
switching euid to 1000 and egid to 1000
considering log /usr/local/app/log/urls.log
log /usr/local/app/log/urls.log does not exist -- skipping ## urls.log is not creating
not running postrotate script, since no logs were rotated
switching euid to 0 and egid to 0
答案1
您可以使用以下copytruncate
选项:
copytruncate
创建副本后就地截断原始日志文件,而不是移动旧日志文件并选择性地创建新日志文件,当某些程序无法被告知关闭其日志文件并因此可能继续写入(追加)到时,可以使用它永远保留以前的日志文件。请注意,复制文件和截断文件之间的时间片非常短,因此可能会丢失一些日志记录数据。 [...]
通过此选项,您可能不一定要用postrotate
位。