2016-02-18 26 views
0

我想配置一个自定义的logrotate策略以每分钟为根目录运行。不幸的是我得到了cron工作的许可,但如果我以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 

和错误消息。

error: error opening /opt/nginx/logs/perf.log: Permission denied

注意:直接运行/etc/custom_logrotate/custom_logrotate.sh工作正常,但面临的问题只是从cron作业。

回答

0

问题在于使用/etc/cron.d而不是/var/spool/cron/root又名crontab。

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