如何检查 logrotate 语法?

Ben*_*992 5 logrotate

我们怀疑 /etc/logrotate.d/FOO 文件中存在拼写错误。

我们如何检查呢?到目前为止我可以看到:

https://linux.die.net/man/8/logrotate

也许是这个?:

logrotate -df /etc/logrotate.d/FOO 2>&1 | grep -i error
Run Code Online (Sandbox Code Playgroud)

Dav*_*uer 8

我同意弗拉德对当前答案的评论。仅检查语法而不实际旋转任何内容的安全方法是使用调试选项。

man logrotate

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

Run Code Online (Sandbox Code Playgroud)

我的机器上的示例:

$ logrotate -d /etc/logrotate.d/httpd 
reading config file /etc/logrotate.d/httpd

Handling 1 logs

rotating pattern: /var/log/httpd/*_log  5242880 bytes (10 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  log does not need rotating
considering log /var/log/httpd/error_log
  log does not need rotating
not running postrotate script, since no logs were rotated
Run Code Online (Sandbox Code Playgroud)

添加该选项将强制实际执行-f旋转(无论是否需要),从而允许进一步分析。

概括:

-d    Just debug, don't actually rotate.
-df   Actually perform rotation and debug (includes verify).
-vf   Actually perform rotation and just verify.
Run Code Online (Sandbox Code Playgroud)


小智 1

logrotate 语法可以在之前使用标志 -v 进行验证

logrotate -vf /etc/logrotate.d/FOO
Run Code Online (Sandbox Code Playgroud)

如果轮换文件出现错误,则应该显示该错误。

  • 注意:此命令还执行日志轮换。 (4认同)
  • 更好的选择可能是“logrotate --debug /etc/logrotate.d/FOO”。检查手册页了解详细信息。 (2认同)