【问题标题】:I'm stuck on logrotate mystery我被困在logrotate之谜
【发布时间】:2015-02-02 12:13:50
【问题描述】:

我有两个 logrotate 文件:

/etc/logrotate.d/nginx-size

/var/log/nginx/*.log 
/var/log/www/nginx/50x.log

{
    missingok
    rotate 3
    size 2G
    dateext
    compress
    compresscmd /usr/bin/bzip2
    compressoptions -6
    compressext .bz2
    uncompresscmd /usr/bin/bunzip2
    notifempty
    create 640 nginx nginx
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

/etc/logrotate.d/nginx-daily

/var/log/nginx/*.log 
/var/log/www/nginx/50x.log

{
    missingok
    rotate 3
    dateext
    compress
    compresscmd /usr/bin/bzip2
    compressoptions -6
    compressext .bz2
    uncompresscmd /usr/bin/bunzip2
    notifempty
    create 640 nginx nginx
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

命令logrotate -d -v /etc/logrotate.d/nginx-sizeoutput:

reading config file /etc/logrotate.d/nginx-size
compress_prog is now /usr/bin/bzip2
compress_options is now  -6
compress_ext is now .bz2
uncompress_prog is now /usr/bin/bunzip2

Handling 1 logs

rotating pattern: /var/log/nginx/*.log 
/var/log/www/nginx/50x.log

 2147483648 bytes (3 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log does not need rotating
considering log /var/log/nginx/error.log
  log does not need rotating
considering log /var/log/nginx/get.access.log
  log does not need rotating
considering log /var/log/nginx/post.access.log
  log needs rotating
considering log /var/log/www/nginx/50x.log
  log does not need rotating
rotating log /var/log/nginx/post.access.log, log->rotateCount is 3
dateext suffix '-20141204'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /var/log/nginx/post.access.log to /var/log/nginx/post.access.log-20141204
creating new /var/log/nginx/post.access.log mode = 0640 uid = 497 gid = 497
running postrotate script
running script with arg /var/log/nginx/*.log 
/var/log/www/nginx/50x.log

: "
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
"
compressing log with: /usr/bin/bzip2

ngnix-daily 上的相同(正常)输出..

如果我从根命令运行

logrotate -f /etc/logrotate.d/nginx-size 

手动完成所有事情。但!它不会自动运行!

选项卡:

*/5 5-23 * * * root logrotate -f -v /etc/logrotate.d/nginx-size 2>&1 > /tmp/logrotate_size   
00 04 * * * root logrotate -f -v /etc/logrotate.d/nginx-daily 2>&1 > /tmp/logrotate_daily

此外,文件 /tmp/logrotate_daily 和 /tmp/logrotate_size 始终为空..

Cron 不要在 /var/log/cron 中给我任何错误

Dec  4 14:45:01 (root) CMD (logrotate -f -v /etc/logrotate.d/nginx-rz-size 2>&1 > /tmp/logrotate_size   )
Dec  4 14:50:01 (root) CMD (logrotate -f -v /etc/logrotate.d/nginx-rz-size 2>&1 > /tmp/logrotate_size   )

这东西有什么问题?.. Centos 6.5 x86_64,Logrotate 3.8.7 版(源代码外)+ logrotate 3.7.8 版(通过 rpm)。

提前谢谢。

【问题讨论】:

  • 您的重定向在这些行中不正确。他们不会将错误信息输出到这些文件。重定向顺序很重要。你想要> /tmp/logrotate_size 2>&1。您是否尝试过在 cron 条目中使用完整路径进行 logrotate?
  • 不,我不要,我现在就做
  • ffs, m8, u r my saver)) 非常感谢,我什至不这么认为
  • 添加你的答案,所以我会把它标记为正确的,plz

标签: nginx cron centos logrotate


【解决方案1】:

您在这些 cron 行中的重定向不正确。他们不会向那些文件输出错误信息。

重定向顺序很重要。你希望>/tmp/logrotate_size 2>&1 得到你想要的。

这里的根本问题是 信息页面的“调试 crontab”部分所涵盖的内容之一。

即“对环境做出假设”。

对环境做出假设

图形程序(X11 应用程序)、java 程序、ssh 和 sudo 在作为 cron 作业运行时是出了名的问题。这是因为它们依赖于交互式环境中可能不存在于 cron 环境中的东西。

要更紧密地以交互方式模拟 cron 的环境,请运行

env -i sh -c '你的命令'

这将清除所有环境变量并运行 sh,这可能在功能上比您当前的 shell 更微薄。

通过这种方式发现的常见问题:

foo:未找到命令或只是 foo:未找到。

$PATH 很可能是在您的 .bashrc 或类似的交互式初始化文件中设置的。尝试通过完整路径指定所有命令(或将 source ~/.bashrc 放在您尝试运行的脚本的开头)。

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    相关资源
    最近更新 更多