【问题标题】:Ubuntu 12.10 logrotate issueUbuntu 12.10 logrotate 问题
【发布时间】:2013-12-12 03:33:31
【问题描述】:

在 Ubuntu 12.10 上,将以下条目添加到 '/etc/logrotate.d' 路径:

# cat /etc/logrotate.d/tsdb
/home/logs/*dat {
        daily
        rotate 30
        compress
        missingok
        notifempty
        create 0664 nagios nagios
}

下面是“/etc/cron.daily/logrotate”文件的样子:

# cat /etc/cron.daily/logrotate
#!/bin/sh

# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

我预计日志会旋转,但我没有看到它们被旋转。我在这里做错了什么?

状态文件确实显示 logrotate 在有问题的文件上运行,但我没有看到任何 旋转日志:

# cat /var/lib/logrotate/status
logrotate state -- version 2
"/home/logs/service-perfdata.dat" 2013-11-26
"/home/logs/host-perfdata.dat" 2013-11-26

另外,'/etc/logrotate.conf' 文件如下所示:

# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

这是手动运行 logrotate 的输出:

# /usr/sbin/logrotate -vd /etc/logrotate.conf

rotating pattern: /home/logs/*dat  after 1 days (30 rotations)
empty log files are not rotated, old logs are removed
considering log /home/logs/host-perfdata.dat
  log does not need rotating
considering log /home/logs/service-perfdata.dat
  log does not need rotating

【问题讨论】:

    标签: ubuntu-12.10 logrotate


    【解决方案1】:

    logrotate 最初处理日志文件的方式并不直观:

    • 第一次运行时,它将日期和日志文件名写入状态文件(它不会轮换任何日志)。
    • 下次运行时,它将检查状态文件以查看是否应轮换日志。

    在你的例子中,你看到它检查了有问题的文件,并在它第一次运行时为每个文件写了一个状态行。任何后续运行都将使用该行上的日期并将其与您的日志文件日期进行比较,以查看它是否应该轮换。我怀疑你的文件的日期是 11 月 26 日,并且在 11 月 26 日检查过,所以它们不需要轮换。回溯您的日志文件,或者回溯状态文件中的条目,都会让它为您轮换文件。

    如果状态文件中没有条目,即使文件需要旋转,它也不会在第一次运行时旋转它们,除非您使用-f(强制)标志。

    【讨论】:

    • 谢谢 Gerrat,这个答案肯定有助于我理解 logrotate。
    猜你喜欢
    • 2013-03-18
    • 2013-04-12
    • 2013-04-01
    • 2012-09-14
    • 2012-11-21
    • 2015-03-29
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多