为什么 logrotate 没有在 aws centos 6.3 上轮换 Tomcat 访问日志?

为什么 logrotate 没有在 aws centos 6.3 上轮换 Tomcat 访问日志?

我有以下文件/etc/logrotate.d

/usr/share/apache-tomcat/logs/*.access {
nodateext
copytruncate
daily
rotate 7
notifempty
postrotate
EXT=`date --date='-1 day' +%Y-%m-%d`
/bin/gzip $1.1
mv $1.1.gz  $1.$EXT.log.gz
endscript
create 0640 tomcat tomcat
}

它应该获取文件 servername.access 并将其移动到 servername.access.2013.02.24.log.gz

当我手动运行时sudo /usr/sbin/logrotate -v /etc/logrotate.d/tomcat它可以工作但是从 cron 每日运行它不起作用

/var/lib/logrotate.status 的内容

logrotate state -- version 2
"/var/log/yum.log" 2013-1-1
"/var/log/up2date" 2012-12-12
"/var/log/sssd/*.log" 2012-12-12
"/var/log/dracut.log" 2013-1-1
"/var/log/httpd/*log" 2013-2-12
"/var/log/wtmp" 2012-12-12
"/var/log/spooler" 2013-2-24
"/usr/share/apache-tomcat/logs/AWS001.access" 2013-2-24
"/var/log/btmp" 2013-2-12
"/var/log/rhsm/rhsmcertd.log" 2013-2-24
"/var/log/maillog" 2013-2-24
"/var/log/php-fpm/error.log" 2013-2-24
"/var/log/cups/*_log" 2012-12-12
"/var/log/secure" 2013-2-24
"/var/log/rhsm/rhsm.log" 2013-2-24
"/var/log/messages" 2013-2-24
"/var/account/pacct" 2012-12-12
"/var/log/cron" 2013-2-24

输出/var/log/cron

Feb 24 00:48:01 AWS001 run-parts(/etc/cron.daily)[13433]: starting logrotate
Feb 24 00:48:02 AWS001 run-parts(/etc/cron.daily)[13457]: finished logrotate

有人有主意吗?

答案1

我终于让它工作了

sudo crontab -u root -l (列出 cron 中用户 root 的作业)
没有工作
sudo crontab -u root -e (编辑 crontab)
@daily /usr/sbin/logrotate -v /etc/logrotate.d/tomcat >/dev/null 2>&1
保存文件
sudo crontab -u 根 -l
root 用户的一个作业

我还在 /etc/logrotate.d/tomcat 中添加了完整路径,如用户 quanta 所示。

注意:当您 cat /etc/crontab 时它仍然是空的

我在文件 /var/spool/cron/root 中找到了工作,
感谢所有回答的人

答案2

当我手动运行时sudo /usr/sbin/logrotate -v /etc/logrotate.d/tomcat它可以工作但是从 cron 每日运行它不起作用

请记住,您应该始终在 cron 中使用绝对路径,因此请尝试以下操作:

EXT=`/bin/date --date='-1 day' +%Y-%m-%d`
/bin/gzip $1.1
/bin/mv $1.1.gz $1.$EXT.log.gz

答案3

logrotate -d /路径/tologrotate.conf

(注意:不是你的片段文件,而是包含它的顶层文件)

其输出可能会有帮助。

我在 /var/lib/logrotate.status 中将日期向后设置一天后,摘录了 sudo /usr/sbin/logrotate -d /etc/logrotate.conf 的内容(否则它不会显示任何旋转内容)

rotating pattern: /usr/share/apache-tomcat/logs/*.access  after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /usr/share/apache-tomcat/logs/AWS001.access
  log needs rotating
rotating log /usr/share/apache-tomcat/logs/AWS001.access, log->rotateCount is 7
dateext suffix '-20130225'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /usr/share/apache-tomcat/logs/AWS001.access.7 to /usr/share/apache-tomcat/logs/AWS001.access.8 (rotatecount 7, logstart 1, i 7), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.6 to /usr/share/apache-tomcat/logs/AWS001.access.7 (rotatecount 7, logstart 1, i 6), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.5 to /usr/share/apache-tomcat/logs/AWS001.access.6 (rotatecount 7, logstart 1, i 5), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.4 to /usr/share/apache-tomcat/logs/AWS001.access.5 (rotatecount 7, logstart 1, i 4), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.3 to /usr/share/apache-tomcat/logs/AWS001.access.4 (rotatecount 7, logstart 1, i 3), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.2 to /usr/share/apache-tomcat/logs/AWS001.access.3 (rotatecount 7, logstart 1, i 2), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.1 to /usr/share/apache-tomcat/logs/AWS001.access.2 (rotatecount 7, logstart 1, i 1), 
renaming /usr/share/apache-tomcat/logs/AWS001.access.0 to /usr/share/apache-tomcat/logs/AWS001.access.1 (rotatecount 7, logstart 1, i 0), 
copying /usr/share/apache-tomcat/logs/AWS001.access to /usr/share/apache-tomcat/logs/AWS001.access.1
truncating /usr/share/apache-tomcat/logs/AWS001.access
running postrotate script
running script with arg /usr/share/apache-tomcat/logs/AWS001.access: "
EXT=`/bin/date --date='-1 day' +%Y-%m-%d`
/bin/gzip $1.1
/bin/mv $1.1.gz  $1.$EXT.log.gz
"
removing old log /usr/share/apache-tomcat/logs/AWS001.access.8
error: error opening /usr/share/apache-tomcat/logs/AWS001.access.8: No such file or directory

相关内容