日志轮换"权限被拒绝"以root身份通过cron

时间:2016-02-18 23:25:20

标签: linux cron logrotate

我正在尝试将自定义logrotate策略配置为以root身份运行每分钟。不幸的是,我得到了cron工作的许可;但如果我以root身份手动运行脚本,我就不会遇到任何问题。

custom_logrotate.sh

#!/bin/bash

/usr/sbin/logrotate -f -v /etc/custom_logrotate/custom_logrotate.conf > /var/log/rotate.log 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

/etc/cron.d/custom_logrotate

* * * * * root /etc/custom_logrotate/custom_logrotate.sh

custom_logrotate.conf

/opt/nginx/logs/stdout.log /opt/nginx/logs/perf.log {
        rotate 10
        copytruncate
        nocompress
        size 10M
        start 0
}

权限

namei -mo /opt/nginx/logs/
f: /opt/nginx/logs/
 dr-xr-xr-x root root      /
 drwxr-xr-x root root      opt
 drwxr-x--- root gnginx capione
 drwxr-x--- root gnginx logs

错误消息。

  

错误:打开错误/opt/nginx/logs/perf.log:权限被拒绝

注意:运行/etc/custom_logrotate/custom_logrotate.sh可以直接正常运行,但只是来自cron作业。

1 个答案:

答案 0 :(得分:0)

问题是使用/etc/cron.d代替/var/spool/cron/root又名crontab。

似乎即使在/etc/cron.d中将用户指定为root,某些实用程序仍然无法正常工作。