Logrotate:每 N 小时运行一次

Ada*_*tan 3 logrotate

考虑一个生成大量日志记录的服务器,这些日志记录使用logrotate和 bz2进行存档:

"/var/log/uwsgi/*/*.log" {
  copytruncate
  daily
  rotate 14
  compress
  delaycompress
  missingok
  notifempty
  compresscmd /bin/bzip2
  uncompresscmd /bin/bunzip2
  compressext .bz2
}
Run Code Online (Sandbox Code Playgroud)

使用hourly轮换不便于实时查看日志(问题往往发生在时间变化时),并且daily轮换经常会导致磁盘满载。

有没有办法将 logrotate 设置为每给定的小时数运行?每 6 小时调用一次对我来说是完美的。

mur*_*uru 6

在 cronjob 中运行logrotate命令-f就足够了。来自man logrotate

-f, --force
      Tells logrotate to force the rotation, even if it doesn't  think
      this  is  necessary.   Sometimes this is useful after adding new
      entries to a logrotate config file, or if  old  log  files  have
      been  removed  by  hand,  as  the new files will be created, and
      logging will continue correctly.
Run Code Online (Sandbox Code Playgroud)

所以编辑/etc/crontab并添加:

30 */6 * * * root logrotate -f /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

更改分钟以满足您的需要。这*/6意味着它应该每六个小时运行一次。


这将轮换所有日志文件,因此将应用程序的设置隔离到(自包含的)配置文件中,然后将其用作参数。例如, create /etc/logrotate.d/uwsgi,包含:

"/var/log/uwsgi/*/*.log" {
  copytruncate
  daily
  rotate 14
  compress
  delaycompress
  missingok
  notifempty
  compresscmd /bin/bzip2
  uncompresscmd /bin/bunzip2
  compressext .bz2
}
Run Code Online (Sandbox Code Playgroud)

以及/etc/logrotate.conf您可能隐式依赖的任何其他行。然后crontab条目将如下所示:

30 */6 * * * root logrotate -f /etc/logrotate.d/uwsgi
Run Code Online (Sandbox Code Playgroud)