如何使日志轮换更改生效

Buf*_*lls 135 linux logrotate syslog

我按照此链接更改了 RHEL 6 的日志轮换配置

对配置文件进行更改后,我该怎么做才能使其生效?

Kiw*_*iwy 186

logrotate使用crontab工作。它是预定的工作,而不是守护进程,因此无需重新加载其配置。
crontab执行logrotate时,会自动使用新的配置文件。
如果您需要测试您的配置,您也可以logrotate使用以下命令自行执行:

logrotate /etc/logrotate.d/your-logrotate-config
Run Code Online (Sandbox Code Playgroud)

或者如评论中所述,识别logrotate命令输出中的crontab -l行并执行命令行参考slm 的答案以获得精确的 cron.daily 解释

  • `logrotate` 有一个 `-d` 选项用于测试(或“调试”),我建议至少运行一次。 (5认同)
  • 只是为了补充您的答案,`logrotate` 的 cron 条目计划每天运行一次。 (2认同)
  • 如果您希望它立即生效,请运行 cron 后记。 (2认同)

slm*_*slm 46

logrotate我在各种发行版上看到的大多数设置都用完了/etc/cron.daily. 那里有一个名为logrotate.

例子

$ ls -l /etc/cron.daily/logrotate 
-rwxr-xr-x 1 root root 180 May 18  2011 /etc/cron.daily/logrotate
Run Code Online (Sandbox Code Playgroud)

手动运行

如果你想让它手动运行,只需以 root 身份运行脚本:

$ sudo /etc/cron.daily/logrotate
Run Code Online (Sandbox Code Playgroud)

如果您查看通常存在的脚本,它会向您展示如何logrotate手动运行,只需运行logrotate+ 其配置文件的路径即可。

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
Run Code Online (Sandbox Code Playgroud)


小智 40

它应该是通过 cron 自动完成的。您可以强制它测试您的更改。

对于全局 logrotate:

sudo logrotate -v -f /etc/logrotate.conf
Run Code Online (Sandbox Code Playgroud)

对于单个 conf 文件:

sudo logrotate -v -f /etc/logrotate.d/someapp.conf
Run Code Online (Sandbox Code Playgroud)

  • `-f` 用于强制旋转,还有 `-d` 用于调试,这也是试运行,它会打印它会做但实际上不会做的所有事情。 (13认同)

小智 6

在我的 CentOS 6.5 机器上为 nginx 设置 logrotate 我必须这样做:

logrotate /etc/logrotate.d/nginx
Run Code Online (Sandbox Code Playgroud)

然后我检查 logrotate 是否像这样处理我的新 nginx 配置:

cat /var/lib/logrotate.status
Run Code Online (Sandbox Code Playgroud)