Ash*_*hok 14 cron logging logrotate cron-task 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)
Ash*_*hok 15
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)
小智 7
我遇到了类似的问题.为了解决这个问题,我首先使用sestatus命令检查了SELinux的状态:
# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
Run Code Online (Sandbox Code Playgroud)
然后,使用ls --scontext检查应用于文件和目录的SELinux安全上下文.检查要logrotate操作的文件,并检查有效的文件,例如/ var/log/maillog:
# ls --scontext /var/log/maillog*
system_u:object_r:var_log_t:s0 /var/log/maillog
system_u:object_r:var_log_t:s0 /var/log/maillog-20140713
system_u:object_r:var_log_t:s0 /var/log/maillog-20140720
system_u:object_r:var_log_t:s0 /var/log/maillog-20140727
system_u:object_r:var_log_t:s0 /var/log/maillog-20140803
Run Code Online (Sandbox Code Playgroud)
使用semanage更改文件上下文.
semanage fcontext -a -t var_log_t <directory/logfile>
restorecon -v <directory/logfile>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19235 次 |
| 最近记录: |