logrotate 旋转失败,root 用户的权限被拒绝

logrotate 旋转失败,root 用户的权限被拒绝

我遇到了一个无法理解的问题。我在 centos 7 服务器上设置了 logrotate 来轮换我的应用程序日志。

该文件位于 /etc/cron.daily 中(我甚至添加了 whoami 来验证它是否以 root 身份运行):

#!/bin/sh

whoami

/usr/sbin/logrotate -v /etc/logrotate.d/myapp
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

这将执行我的 logrotate 配置...

"/opt/myapp/log/*.log" {
  daily
  create 644 root root
  rotate 5
  size 20M
  copytruncate
  missingok
  notifempty
  compress
}

这是我的日志目录...

[root@server log]# ls -la
total 97684
drwxr-xr-x. 3 root root      4096 Sep 28 12:41 .
drwxr-xr-x. 5 root root      4096 Sep 27 20:57 ..
-rw-r--r--. 1 root root 100015457 Sep 28 12:36 myapp.log

每次我们以 root 身份手动运行它时,它都会轮换日志……但是当我们通过 cron 运行它时,它会失败。这是它发送的邮件……

From root@server  Thu Sep 28 12:30:02 2017
Return-Path: <root@server>
X-Original-To: root
Delivered-To: root@server
Received: by server (Postfix, from userid 0)
        id 9C82B419; Thu, 28 Sep 2017 12:30:02 -0500 (CDT)
From: "(Cron Daemon)" <root@server>
To: root@server
Subject: Cron <root@server> run-parts /etc/cron.daily
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=10746>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20170928173002.9C82B419@server>
Date: Thu, 28 Sep 2017 12:30:01 -0500 (CDT)
/etc/cron.daily/myapp:

root <<<<<<<<< whoami output
reading config file /etc/logrotate.d/myapp
Handling 1 logs
rotating pattern: /opt/myapp/log/*.log  20971520 bytes (5 rotations)
empty log files are not rotated, old logs are removed
considering log /opt/myapp/log/myapp.log
  log needs rotating
rotating log /opt/myapp/log/myapp.log, log->rotateCount is 5
error: error opening /opt/myapp/log/myapp.log: Permission denied
set default create context

有人遇到过这种情况或知道是什么原因造成的吗?

答案1

事实证明这是一个 SELinux 问题,文件上下文不是 logrotate 可以访问的,因此反复被拒绝访问。

请参阅此处以了解此答案:SELinux 上下文

相关内容