log等を週単位で別ファイルに管理し、規定時期を過ぎると削除する仕組みとして logrotate機能がある。これはcronを利用して設定されている。
cronはcrontabで指定した以外にも、/etc/cron.dailyや/etc/cron.weekly等の中のシェルを毎日または毎週実行する。
cronは/etc/crontabに指定のある日時に実行される。例えばデフォルトでは以下の ように設定されている。
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/
# run-parts 01 * * * * root run-parts /etc/cron.hourly <--毎時:01分 02 4 * * * root run-parts /etc/cron.daily <--毎日:04時02分 22 4 * * 0 root run-parts /etc/cron.weekly <--毎週:日曜:04時22分 42 4 1 * * root run-parts /etc/cron.monthly <--毎月:1日:04時42分
logrotateはまず、/etc/cron.daily/logrotateにより
#!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf
と指定されているため、/etc/logrotate.confを参照して実行される
/etc/logrotate.confには
# see "man logrotate" for details
# rotate log files weekly
weekly <--毎週
# keep 4 weeks worth of backlogs
rotate 4 <--過去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.
とあるので、基本的には毎週、過去4回分を残す指示がある。さらに細かい指定は
/etc/logrotate.dの中に指定があることが、指示されている。そこで/etc/logrotate.d
を見るといくつか設定ファイルがある。
サンプルとして/etc/logrotate.d/apacheを見ると
/usr/local/apache2/logs/access_log {
^^^^^^^^^^^^^^分割logの指定
missingok
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^動作しているpidが保存されているファイルを指定
endscript
}
/usr/local/apache2/logs/agent_log {
missingok
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
/usr/local/apache2/logs/error_log {
missingok
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
/usr/local/apache2/logs/referer_log {
missingok
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
/usr/local/apache2/logs/ssl_request_log {
missingok
postrotate
/bin/kill -HUP `cat /usr/local/apache2/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
のようにlog保存ファイルと、動作pidがあるファイルを指定する。
設定ファイルが正しく機能するか否かは、cron.daily(注)が実行されるまで分からない なお、-dオプションを付けてlogrotateコマンドを実行した場合、ログファイルは変更されない。
エラーの場合
# /usr/sbin/logrotate -dv /etc/logrotate.d/openmeetings
reading config file /etc/logrotate.d/openmeetings
reading config info for /var/log/openmeetings.log
error: /etc/logrotate.d/openmeetings:3 lines must begin with a keyword or a filename (possibly in double quotes)
error: /etc/logrotate.d/openmeetings:4 lines must begin with a keyword or a fi (略)
Handling 1 logs rotating pattern: /var/log/openmeetings.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /var/log/openmeetings.log log does not need rotating
たぶんOK
# /usr/sbin/logrotate -dv /etc/logrotate.d/openmeetings reading config file /etc/logrotate.d/openmeetings reading config info for /var/log/openmeetings.log Handling 1 logs rotating pattern: /var/log/openmeetings.log 1048576 bytes (no old logs will be kept) empty log files are not rotated, old logs are removed considering log /var/log/openmeetings.log log does not need rotating not running postrotate script, since no logs were rotated