logrotate cron 作业不旋转某些日志

use*_*396 4 logging logrotate cron cron.daily log-rotation

我在“logrotate.d”目录中添加了两个脚本,以便轮换我的应用程序日志。这是其中之一的配置:

<myLogFilePath> {
  compress
  copytruncate
  delaycompress
  dateext
  missingok
  notifempty
  daily
  rotate 30
}
Run Code Online (Sandbox Code Playgroud)

“cron.daily”目录中有一个“logrotate”脚本(根据cron日志,它似乎每天都在运行):

#!/bin/sh

echo "logrotate_test" >>/tmp/logrotate_test
#/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
/usr/sbin/logrotate -v /etc/logrotate.conf &>>/root/logrotate_error

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

第一个 echo 语句正在运行。
但是我发现我的应用程序日志本身没有轮换,而其他日志(如 httpd)正在轮换 **
**而且我也没有在提到的“logrotate_error”文件中看到任何输出
(对所有用户都有写权限)。

但是系统日志显示:“logrotate:ALERT 异常退出 [1]”

但是当我手动在“cron.daily”脚本中运行相同的“logrotate”时,一切似乎都正常。

为什么它在每日 cron 计划中不轮换?我在这里做错了吗?
如果我能得到这些急需的帮助,那就太好了。

更新: 看起来,这是因为 selinux - 我的用户主目录中的日志文件受到 selinux 的限制,并且运行 logrotate 脚本时:

SELinux is preventing /usr/sbin/logrotate from getattr access on the file /home/user/logs/application.log
Run Code Online (Sandbox Code Playgroud)

use*_*396 8

SELinux 限制了对没有所需 SELinux 文件上下文类型的目录中日志文件的 logrotate 的访问。"/var/log" 目录有"var_log_t"文件上下文,logrotate 能够完成所需的工作。所以解决方案是在我的应用程序日志文件和它的父目录上设置它:

semanage fcontext -a -t var_log_t <directory/logfile>
restorecon -v <directory/logfile>
Run Code Online (Sandbox Code Playgroud)

  • 有关问题和解决方案的完整说明,请参阅 https://access.redhat.com/solutions/39006。 (3认同)