RedHat Linux でログローテーション(デフォルト)




RedHat Linux におけるログのローテーションの仕組みは次の通り。

ログ messages に注目して見てみる。
デフォルトでは、週に一回、日曜日の 4:02 にローテーションがかかっている。
しかも4世代保管。

# ls -al /var/log/messages*
-rw------- 1 root root 2091 7月 20 09:33 messages
-rw------- 1 root root 40896 7月 16 04:02 messages.1
-rw------- 1 root root 48533 7月 9 04:02 messages.2
-rw------- 1 root root 15183 7月 2 04:02 messages.3
-rw------- 1 root root 3841 6月 25 04:02 messages.4

これを実現しているのは、ユーザの cron ではないようだ。
デフォルトでは何も登録されていない。

# crontab -l

/etc/cron.weekly の中を見てみる。

# ls -al /etc/cron.weekly/

何もない。

/etc/cron.daily の中を見てみる。

# ls -al /etc/cron.daily/
drwxr-xr-x 2 root root 4096 7月 6 17:39 .
drwxr-xr-x 56 root root 4096 7月 14 17:27 ..
lrwxrwxrwx 1 root root 28 12月 1 2005 00-logwatch -> ../log.d/scripts/logwatch.pl
-rwxr-xr-x 1 root root 276 1月 25 2003 0anacron
-rwxr-xr-x 1 root root 51 1月 25 2003 logrotate
-rwxr-xr-x 1 root root 418 2月 11 2003 makewhatis.cron
-rwxr-xr-x 1 root root 104 2月 28 2003 rpm
-rwxr-xr-x 1 root root 132 1月 22 2004 slocate.cron
-rwxr-xr-x 1 root root 193 2月 11 2003 tmpwatch

logrotate というシェルスクリプトがある。ファイルを見てみると、

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
(省略)

/usr/sbin/logrotate プログラムを /etc/logrotate.conf という設定ファイルで動かしている。
/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

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

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

# system-specific logs may be also be configured here.

weekly という文字が見つかった。
messages という文字は見当たらないが、
include /etc/logrotate.d と指定して /etc/logrotate.d ディレクトリの中に
置いてあるファイルの内容を展開して読み込んでいる。

/etc/logrotate.d を見てみる。

# ls -al /etc/logrotate.d
drwxr-xr-x 2 root root 4096 7月 6 17:39 .
drwxr-xr-x 56 root root 4096 7月 14 17:27 ..
-rwxr-xr-x 1 root root 161 7月 17 2005 cups
-rw-r--r-- 1 root root 61 2月 28 2003 rpm
-rw-r--r-- 1 root root 543 11月 28 2005 squid
-rw-r--r-- 1 root root 228 6月 16 2004 syslog
-rw-r--r-- 1 root root 32 2月 23 2001 up2date
-rw-r--r-- 1 root root 95 3月 1 2003 vsftpd.log

syslog というファイルがある。見てみると、

/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

/var/log/messages が見つかった。

調べたことをまとめると、
/etc/crontab の定義により、
/etc/cron.daily/logrotate が毎日 4:02 に動いている。

したがって、/etc/cron.daily/logrotate の中で
/etc/logrotate.conf の内容にしたがってログローテーションが
毎日 4:02 に行われるはずだが、
/etc/logrotate.conf の中でグローバルな設定として weekly と指定しているので、
個別に daily と指定しない限り、
週に一回、日曜日の 4:02 にしかログローテーションが実行されていない。

「週に一回、日曜日」というタイミングを変更したい場合は、
/etc/logrotate.d の中にあるファイルに個別に daily あるいは monthly を追記すれば良い。
個別な設定は /etc/logrotate.conf の中のグローバルな設定より優先して反映される。

「4:02」という時間を変更したい場合は、/etc/crontab の時間を変更すれば良い。

また、ローテーションの対象は、/etc/logrotate.d の中に
書式に則って作成した任意のファイルを
置いておけば読み込まれる。

/etc/logrotate.d を使用しない場合は、
/etc/logrotate.conf に直接記載することも可能。