【问题标题】:logrotate: daily rotation didn't work even though i used -f optionlogrotate:即使我使用了 -f 选项,每日轮换也不起作用
【发布时间】:2018-09-06 03:24:04
【问题描述】:

这是 /etc/logrotate.d/mylog 下的 logrotate 配置文件

/var/www/www.mysite.com/logs/* {
        daily
        rotate 365
        compress
        delaycompress
        missingok
        notifempty
        create 0664 apache apache
}

我昨天下午 4 点设置的,今天上午 10 点我没有看到任何压缩文件。

我尝试使用 -d 和 -f 选项进行 logrotate

使用 -d 选项,我得到了这个输出

reading config file mysite
reading config info for /var/www/www.mysite.com/logs/*

Handling 1 logs

rotating pattern: /var/www/www.mysite.com/logs/*  after 1 days (365 rotations)
empty log files are not rotated, old logs are removed
considering log/var/www/www.mysite.com/logs/access-2018-08-08.log
  log does not need rotating

使用 -f 选项,什么也没有出现。

但我注意到它在我的每个日志中添加了 .1 后缀。

请任何人解释一下这是什么奇怪的行为?

【问题讨论】:

标签: linux logrotate


【解决方案1】:

Logrotate 没有内置的调度系统。

如果您想手动清理一次日志,请确保您可以手动运行它:

logrotate /etc/logrotate.d/mylog

如果您检查 logrotate 手册页中的 -f 行为:

-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.

-d, --debug
   Turns on debug mode and implies -v.  In debug mode, no changes will be made to 
   the logs or to the logrotate state file.

因此,使用 -f 选项,您可以强制轮换所有日志,即 .1 后缀。

使用 -d 选项,这是一次试运行,因此您只能预览更改。

现在如果你想每天运行它,你必须使用 cron。

安装后默认情况下logrotate应该有一个默认配置文件/etc/logrotate.conf和一个默认的cron作业/etc/cron.daily/logrotate

您可以使用它们,也可以创建自己的配置文件和 cron 作业。

如果您将自定义配置放置在 /etc/logrotate.d/mylog 中,那么您可以将以下 cron 作业放置在 /etc/cron.d/logrotate-cron em> 每天运行它。

0 0 * * * root /usr/sbin/logrotate /etc/logrotate.d/mylog --state /etc/logrotate.status

您可以检查文件 /etc/logrotate.status 以确保它运行。

【讨论】:

  • 嗨@Victor Wong 非常感谢您的回答。我会尝试 cron 工作。还有一个小问题,使用 * 表示我希望所有日志都被轮换是否正确? mysite/logs 目录下有一些不同的日志格式。例如 www.mysite.com-access_log.20180831 error_log access_log
  • 乐于助人。是的,您可以使用通配符,但它不会自动扩展子目录。因此,您将需要多个条目,例如 /var/log/* /var/log/*/* 甚至 /var/log/*/*/*。如果它解决了您的问题,请点赞并将其标记为答案。
猜你喜欢
  • 1970-01-01
  • 2019-06-17
  • 2018-10-12
  • 2020-04-17
  • 2011-10-30
  • 1970-01-01
  • 2014-10-18
相关资源
最近更新 更多