使用logrotate每小时轮换一次文件?

Kis*_*ore 58 linux cron logrotate rhel

我尝试设置位于/ tmp/hr_logs /的日志文件的日志轮换.为了设置,我在linux中使用了logrotate,我可以在/etc/logrotate.conf中使用以下配置每天轮换它

  /tmp/hr_logs {
  daily
  rotate 4
Run Code Online (Sandbox Code Playgroud)

使用此配置,文件将每天轮换,系统将保留4个附加日期的日志文件[格式:-YYYYMMDD]

现在,我尝试设置一组不同的日志文件,我需要按小时进行旋转,为此,我已经完成了logrotate.conf中的配置:

  /tmp/last_logs {
  hourly
  rotate 4
Run Code Online (Sandbox Code Playgroud)

但这根本不起作用?有人可以请你指导我吗?

sca*_*cai 80

该联机帮助页logrotate.conf包含对每小时选项的重要建议:

日志文件每小时轮换一次.请注意,通常logrotate配置为每天由cron运行.您必须更改此配置并每小时运行logrotate才能真正每小时轮换一次日志.

正如yellow1pl所指出的,解决方案是将文件复制/etc/cron.daily/logrotate/etc/cron.hourly/目录中.这至少适用于Debian和可能的Debian派生.

  • 复制或移动文件?另外,我不应该在 /etc/logrotate.conf 中将“每天”更改为“每小时”吗? (6认同)
  • 我会符号链接,就好像脚本更新一样,您将继承任何更改。此外,如果您使用 dateext 选项,则需要重新格式化扩展名,以便每天可以有多个 uniq 文件名。我使用以下内容。dateext 日期格式 -%Y%m%d-%s (3认同)
  • 我用于符号链接的命令:`ln -s /etc/cron.daily/logrotate /etc/cron.hourly/logrotate` (3认同)
  • 这也适用于CentOS (2认同)

rsm*_*rsm 18

/etc/cron.daily/logrotate每日logrotates 都有脚本.但是,/etc/cron.hourly/目录中默认没有此类脚本.复制此脚本,它应该可以正常工作.

  • 难道你不想移动`logrotate`而不是复制?否则logrotate会同时运行两次 (9认同)
  • @mulllhausen `logrotate` 默认情况下会锁定状态文件,如果锁定已被锁定,则退出 - 因此,即使同时运行在 `cron.hourly` 和 `cron.daily` 计划中也没有问题。 (5认同)
  • 我发现在将`logrotate`移动到`/ etc/cron.hourly`后我必须重新启动`crond`服务.例如在CentOS 7`sudo systemctl restart crond`中 (4认同)