トップページ>>>「いろいろ」目次

logrotateと戯れて・・・。

初版作成:2002/10/22

目次

  1. 前書き
  2. 本題
  3. 後書き或いは感想

前書き

 あー・・・。まあ、いい加減Workstationとして何度も電源切ったり入れたりしているうちにですね、ログファイルが気になってきたのですよ。
 んで、以前いじくったことのあるlogrotateとゆープログラムをカスタマイズしようと思い立った訳ですね。
 logrotateとゆーのは結構昔から使われているらしい、ログファイルの自動バックアップをしてくれるプログラムです。/var/logの中にmessageに続いてmessage.1, message.2, ...とあるでしょ?あんな風に、連番みたいにして自動的に長くなったログファイルを切り分け(ログローテーション)てくれるのがlogrotateです。
 これ、結構知ってると便利なので。んで、大抵はcronと組み合わせて使うので。
 cronの設定の復習もかねてlogrotateと戯れてみました。
 環境はturbolinux Workstation 8です。
 長くなりますけど、おつきあいください。
目次に戻る

本題

  1. logrotateについて
  2. cronについて
  3. cronのカスタマイズ
  4. logrotateのカスタマイズ
  5. 資料:TLXW8のcron初期設定
  6. 資料:TLXW8のlogrotate初期設定
  7. 資料:logrotate実行時オプション(「man logrotate」より抜粋)
  8. 資料:logrotate設定ファイルオプション(「man logrotate」より抜粋)

logrotateについて
「本題」に戻る
 まず、logrotateはどこにあるのでしょうか。whereisで・・・
/usr/sbin/logrotate
 にありました。それではRPMはどれか?
[fenjin@seisyuu fenjin]$ rpm -qf /usr/sbin/logrotate
logrotate-3.3.2-5
 はい。それではコレのパッケージ情報を見てみませう。
[fenjin@seisyuu fenjin]$ rpm -qil logrotate
Name        : logrotate  Relocations: (not relocateable)
Version     : 3.3.2      
Vendor: Turbolinux Japan K.K 
Release     : 5   Build Date: Sun Apr 28 21:15:17 2002
Install date: Tue Sep 10 07:28:20 2002
Build Host: y.jp.tlan
Group       : System Environment/Base 
Source RPM: logrotate-3.3.2-5.src.rpm
Size        : 39076  License: GPL
Packager    : Turbolinux Japan K.K 
Summary     : Rotates, compresses, removes and mails system log files.
Description :(省略)

Install the logrotate package if you need a utility to deal with the
log files on your system.
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/usr/sbin/logrotate
/usr/share/man/man8/logrotate.8.gz
 ・・・こんだけです。あっさりとしたもんだ。まあ、でもその分分かりやすくて好感が持てます。まあ大体見当がつくと思いますが、/etc/cron.daily/logrotateがcron用のシェルスクリプト。/etc/logrotate.confと/etc/logrotate.confが実行時の設定ファイル。と相成ります。ここら辺は資料として後ろの方に載っけときましたので、眺めてみてください。特にcron.daily/logrotateときたら・・・。あっさりしすぎ。

 さて。結局/usr/sbin/logrotateに設定ファイルのフルパスを引数と渡して起動すればよさげです。オプションの詳細については後で「資料:logrotate実行時オプション(「man logrotate」より抜粋)」を眺めてみてください。
 例えば自分で創ったオリジナルの設定ファイルが /etc/logrotate_local.confとかにあれば、
# /usr/sbin/logrotate /etc/logrotate_local.conf
 とすれば良いわけです。んで、設定ファイルの文法をチェックしたり、実際にログを切り分けないけどどんな風に動くのかシミュレートだけしてみたいときは
# /usr/sbin/logrotate -d 設定ファイル
 実際の動作チェックもしたいし、強制的にログを切り分けたいときは
# /usr/sbin/logrotate -f 設定ファイル

 ・・・とまあ。logrotate自体の実行時オプションはそんなに難しくないです。

 では・・・肝心の、「どのログファイルをどうローテーションさせるのか」についての設定を見てみますか。後ろの方に載っている、「資料:logrotate設定ファイルオプション(「man logrotate」より抜粋)」をちょっと眺めてきてください。(TLXW8ではcronの設定から、/etc/logrotate.confを設定ファイルとしてlogrotateは起動しています)
 ・・・眺めてきました?それじゃあ、実際のファイル構成について簡単に概要を・・・。(「資料:TLXW8のlogrotate初期設定」
 まず、グローバルな。つまり全体にとりあえず適用される設定はTLXS7, TLXW8, RHL7.2共に、/etc/logrotate.confファイルで行われています。んで。この中で
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
 っていうところがありますよね?includeによって、/etc/logrotate.dディレクトリ内のファイルが全て設定ファイルとして扱われるようになります。実際、apacheだのなんだのと、それっぽいのが詰まっています。
 んで。logrotate.confで全体設定が行われますが、各ログファイル個別の設定はどこで行われているのか。それは例えばlogrotate.conf内の
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}
 とか、各設定ファイル内の
[root@seisyuu logrotate.d]# cat apache
/var/log/httpd/access_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}
 で行われます。よーするに、
(ログファイル名){
  この中で必要なオプションだけ、個別に上書きできる。

 という仕組みです。

 ここまでくればカスタマイズの見当もついてきましたよー。
1.独自にカスタマイズした/etc/logrotate_local.confみたいなのを創る。
2.ついでに独自にカスタマイズした/etc/logrotate_local.d/を創って、そん中に個別のログファイル設定を突っ込んで、/etc/logrotate_local.confに読み込ませる。
3.最後にcronのlogrotateシェルスクリプトを変更するか、追加して、/etc/logrotate_local.confを読み込んで起動するようにする。

 さあ、ほんじゃあやってみますか・・・って、ちょっと待って。
 じつわ。TLXS7, TLXW8, RHL7.2共にcronに関して注意すべきワンポイントがあるのですよ。他にも、ワークステーション仕様のlogrotateの場合、夜中の4時に動き出すようなcron任せにもできませんし。
 というわけで、急がば回れでちょっとcronについて勉強、或いは復習します。
参考資料
・「man logrotate」

cronについて
「本題」に戻る
 初心者の方のために、「cron」について簡単に説明します。
 cronとはデーモン(サーバーじゃないな。ネットワークからの入力を受け付けないから。)として、バックグラウンドで動くプログラムです。んで、設定ファイル(大概は/etc/crontab)に従って、指定された時刻や曜日、日付になったら、これまた設定ファイルに指定されているスクリプトを実行していきます。
 よく、夜中の4時頃突然ハードディスクががりがり言い出したりしませんか?他にも久しぶりにrootでログインしてみたら、なんかログファイルのチェックみたいな内容のメールが大量にたまっていたり。これらがcronのお仕事です。別にパソコンがおかしくなった訳じゃあありません。
 これらはcronが多くのディストリビューションで毎日午前4時、いろいろなバックエンド処理を行うように設定されているからです。んで、それらの記録をroot宛にメールで知らせるよう、それぞれのバックエンド処理を行うプログラムが設定されているからです(例えばファイル改ざんのチェックだったり、logrotateだったり)。

 これらは別にサーバーだったら何の害もないんですよ。常時稼働しますから。
 ところが、夜中の4時過ぎまで電源を入れることなんて滅多にないワークステーションではこれら、Linuxが健全に保つように設定された各種バックエンドプログラム(シェルスクリプト)が動きません。これはちょっとまずい。(深夜業務に使われているワークステーションは別です)
 ファイル改ざん検知プログラムはまだ良いんです。ワークステーションで、自分一人しか使わなかったり、サーバーを立てたりはしてない場合はあんまり必要ないです。
 問題は、このページの本題でもあります「logrotateによるログの切り分け」です。
 気がつけば/var/log/messagesが数百KB、あるいはMBにまで膨らんでいた・・・なーんてことがまま、あり得ます。
 ログファイルは、各種ソフトの調子(機嫌とも言う)を見たり、何か問題が起こったりした場合に必須の情報源です。それが数百KBもあると、必要な情報を探すのも一苦労になります。lessコマンドも遅くなりますし。
 まあ、そういうお題でこのページが創られたわけです。この解決はもう少し先にして、先にcronについて確認しておきましょう。

 cronの構成を確認しておきます。
[fenjin@seisyuu fenjin]$ rpm -qa | grep cron
crontabs-1.10-2
vixie-cron-3.0.1-50
[fenjin@seisyuu fenjin]$ rpm -qil crontabs
Name        : crontabs                     Relocations: (not relocateable)
(省略)
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab
/usr/bin/run-parts
 /etc/crontabというのがcronの読み込むファイル。つまりこのパッケージはcronのTLXW8仕様の設定ファイル集と言うことですな。
 肝心のcron実行ファイルは?「less /etc/rc.d/init.d/crond」を眺めると、crondというのがdaemonとして実行されています。そこで・・・
[fenjin@seisyuu init.d]$ whereis crond
crond: /usr/sbin/crond /usr/share/man/man8/crond.8.gz
[fenjin@seisyuu init.d]$ rpm -qf /usr/sbin/crond
vixie-cron-3.0.1-50
[fenjin@seisyuu init.d]$ rpm -qil vixie-cron
Name        : vixie-cron
(省略)
/etc/cron.d
/etc/logrotate.d/cron
/etc/rc.d/init.d/crond
/usr/bin/crontab
/usr/sbin/crond
/usr/share/doc/packages/vixie-cron-3.0.1
(省略)
/usr/share/doc/packages/vixie-cron-3.0.1/THANKS
/usr/share/man/man1/crontab.1.gz
(省略)
/var/spool/cron
 ちょっと勉強不足で分からなかったのですが、/etc/cron.d/ディレクトリの中にはなーんも入っていなかったのです。何のためなんでしょ?今回はあくまでもlogrotateのカスタマイズでしたので、cronは基本的にcrontabで制御できることが分かれば十分なので。あまり調べませんでした。
 TLXW8のcrontabの詳細は「資料:TLXW8のcron初期設定」を参照してください。とりあえず、ここではman 5 crontab より日時指定をメインに抜粋しておきます。
フィールド 指定可能な値
---------- --------------
分         0-59
時         0-23
月内日     0-31
月         0-12 (もしくは名前。下記を参照)
曜日       0-7 (0 と 7 は日曜日。もしくは名前)
・・・
CRON ファイルの例
       # (/etc/passwd の指定に関らず) コマンド実行に /bin/sh を使用する。
       SHELL=/bin/sh
       # (この crontab の所有者に関らず) あらゆる出力を `paul' にメールする。
       MAILTO=paul
       #
       # 毎日、日付変更の 5 分後に実行する
       5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # 毎月初日の 2:15pm に実行する -- 出力は paul にメールされる
       15 14 1 * *     $HOME/bin/monthly
       # 週末の午後 10 時に実行してジョーを心配させる
       0 22 * * 1-5 mail -s "午後10時だ" joe%ジョー、%%お前の子どもはどこだい?%
       23 0-23/2 * * * echo "毎日 0,2,4..時 23 分に実行する"
       5 4 * * sun     echo "日曜 4 時 5 分に実行する"
参考資料 ・「man cron」
・「man crontab」

cronのカスタマイズ
「本題」に戻る
 え?いい加減疲れてきた?
 も、もうちょっと・・・!これやって、あとlogrotateのカスタマイズだけだから!
 疲れたら本HPのリンクページにあるCG系サイトにでも寄り道して、一休みしてくださいませ。

 さて。ここは非常に簡単です。
 ワークステーション仕様で面倒なのは、「root宛に毎時送られてくるファイル改ざんだのログファイルだののレポートメール」だけです。
 つまり。cron.hourlyが邪魔なわけです。
 じゃあ後は簡単。crontab中の
# run-parts

01 * * * * root run-parts /etc/cron.hourly	#毎時間01分に実行
02 4 * * * root run-parts /etc/cron.daily	#毎日午前四時02分に実行
 を、
# run-parts

#01 * * * * root run-parts /etc/cron.hourly	#毎時間01分に実行
02 4 * * * root run-parts /etc/cron.daily	#毎日午前四時02分に実行
 でお仕舞い。毎時間行う部分をコメントアウトしちゃう。
 あとは、お好みでcron.hourlyの毎時設定を変更して、cron.dailyと同じく毎日設定にしてしまうとか、毎日設定でも実行する時間を(ほぼ確実に電源が入っているだろうと思われる)適当な時間に変更するとか。
 そんなところでしょう。
 はい、cronのカスタマイズ終わり!いよいよlogrotateのカスタマイズだー!!

logrotateのカスタマイズ
「本題」に戻る
 お待たせしました。いよいよlogrotateをカスタマイズして快適なログ生活を送りましょう。まず、今回私がカスタマイズしたポイントを説明します。

1.cronに設定済みのlogrotate.confは依然として使い続けるので、いじらない。
2.独自の設定ファイル、/etc/logrotate_local.confと、ログファイル毎の設定ファイルを突っ込むための/etc/logrotate_local.d/ディレクトリを作る。
3.基本は/etc/logrotate.conf、/etc/logrotate.d/で。これらを上記ファイルやディレクトリにコピーして、カスタマイズする。
4.ローテーションは基本は9回まで。*.log,*.log.1,*.log.2,...*.log.9までローテーションさせる。古くなったログファイルの圧縮作業などはなし。
5.対象ログファイルが無くても無視して次のログファイルへ。つまり、missingok。
6.ローテーションさせる条件は、基本的にログファイルのサイズが200KBを越えたら。

 以上の要求に従って、オリジナルのlogrotate.confやlogrotate.dから削ったり、付け足したりした結果を以下に示します。
[fenjin@seisyuu etc]$ cat /etc/logrotate_local.conf
#
# Default configuration is not proper for Workstation.
# This configuration checkes the size of every log files.
# And executed by /etc/rc.d/rc.local so, every time when machine start ups,
# logrotate which use this configuration will automatically executed.

# rotate log files daily
daily

# default size of log files which should be rotated is bigger than 200KB
size 200k

# Log files are rotated 9 times.
rotate 9

# send errors to root
errors root

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

# Original Log Rotate Configuration files are included in this directory
include /etc/logrotate_local.d

# no packages own lastlog or wtmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}
 ・・・と、ここまでがlogrotate_local.confです。includeで/etc/logrotate_local.dディレクトリの中身を読みとります。では、その中は・・・
[fenjin@seisyuu etc]$ cd /etc/logrotate_local.d/
[fenjin@seisyuu logrotate_local.d]$ ls
apache  syslog

[fenjin@seisyuu logrotate_local.d]$ cat apache
# ソースコードからビルドしたApacheにあわせてあります。
/usr/local/apache/logs/access_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/usr/local/apache/logs/agent_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/usr/local/apache/logs/error_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/usr/local/apache/logs/referer_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

[fenjin@seisyuu logrotate_local.d]$ cat syslog
# デフォルトのsyslogを修正して、必要なログファイルだけ、
# 不必要な処理を省いて設定し直しました。
/var/log/messages {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/secure {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/maillog {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/spooler {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/boot.log {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}
 で・・・logrotate_local.conf中のコメントにもありますが、これはWorkstation仕様ですので、できれば「電源が入るたびに」ログローテーションしてもらいたいところです。そういった起動条件にはcronは不適切です。というわけで、/etc/rc.d/rc.localというスクリプトファイルの末尾辺りに、次の一行を追加します。

/usr/sbin/logrotate /etc/logrotate_local.conf
echo executed /usr/sbin/logrotate /etc/logrotate_local.conf. Done.
sleep 3

 最初の一行で/etc/logrotate_local.confを設定ファイルとしてlogrotateを起動しています。んで、確認用のメッセージにechoを使ってます。
 最後の「sleep 3」というのは、echoメッセージがあっという間に流れ去ってしまいエラーなどがあっても見えない。また、apacheやsyslogなどを何度か再起動しているのでその分の時間取りもあった方が良いんじゃないか?という考えから3秒ほどストップさせることにしたわけです。
 rc.localについてですが。/etc/rc.d/以下に、rc*.dというディレクトリに各ランレベルに入ったときに実行されるスクリプトへのリンクが格納されています。ここら辺は昨今のLinux雑誌に何度も紹介されていますのでそちらを参照してください。
 で、とにかくrc.localというのはランレベル毎のスクリプトを全部起動し終わり、後はログインプロンプトを表示するだけ、という、最後の最後に起動されるスクリプトファイルです。このスクリプトファイルが実行し終えた次の瞬間にはもう、ログインプロンプトが表示されます。で、rc.localはユーザーが自由にカスタマイズすることが許されているスタートアップシェル(とでも言うのかな?)でもあります。
 そんなわけで、今回「電源が入るたびに起動する」という要求を、rc.local内でlogrotateを実行する、という形で実現しました。

 さて、実際に上記の設定でどんな具合か。
 結論は、「なかなかいい感じ。」です。200KBというのはlessコマンドで流し流し読める私にとっての限界量ですが、まあ。messageに関してはいい感じです。maillogとsecureに関してはそもそも使ってないので(sshで多少secureが増える)無事、ローテーションされませんでした。boot.logはよく分かりません。一応、200KBには達してないのでローテーションされていませんし。
 Apacheのログファイルも、まだ本格使用してないのであんましたまっておらず、ローテーションされていません。
 盲点だったのが/var/log/samba/。まだ50KBくらいですが、実際問題私は毎日毎日VMware越しにSambaを利用しているわけですから、放っておくわけにも参りません。が、Sambaを利用し初めてかれこれ2週間以上経過していて50KB程度ですから、もうちょっと様子を見てもよいかもしれません。

 それではワークステーションとして使っているみなさんも、快適なログ生活を送ってください。
総合参考資料 ・「man logrotate」
・「man cron」
・「man crontab」


資料:TLXW8のcron初期設定
「本題」に戻る
service: /etc/rc.d/init.d/cron

configuration files:

/etc/crontab
[root@seisyuu etc]# cat 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	#毎日午前四時02分に実行
22 4 * * 0 root run-parts /etc/cron.weekly	#毎週日曜日午前四時22分に実行
42 4 1 * * root run-parts /etc/cron.monthly	#毎月一日午前四時42分に実行

/etc/cron.daily/
[root@seisyuu cron.daily]# ls
inn-cron-expire*  inn-cron-rnews*  logrotate*  makewhatis.cron*
 medusa.cron*  slocate.cron*  tetex.cron*  tmpwatch*
[root@seisyuu cron.daily]# cat logrotate
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf

/etc/cron.hourly/
[root@seisyuu cron.hourly]# ls
inn-cron-nntpsend*  logcheck*  rmmod*
[root@seisyuu cron.hourly]# cat inn-cron-nntpsend
#!/bin/sh
/sbin/chkconfig innd && su - news -c /usr/bin/nntpsend
[root@seisyuu cron.hourly]# cat logcheck
#!/bin/sh
/usr/sbin/logcheck
[root@seisyuu cron.hourly]# cat rmmod
#!/bin/sh
/sbin/rmmod -as
[root@seisyuu cron.hourly]# file /usr/sbin/logcheck
/usr/sbin/logcheck: Bourne shell script text executable

/etc/cron.weekly/
[root@seisyuu cron.weekly]# ls
makewhatis-ja.cron*  makewhatis.cron*
[root@seisyuu cron.weekly]# cat makewhatis-ja.cron
#!/bin/bash
/usr/sbin/makewhatis /usr/share/man/ja_JP.eucJP
exit 0
[root@seisyuu cron.weekly]# cat makewhatis.cron
#!/bin/bash

LOCKFILE=/var/lock/makewhatis.lock

# the lockfile is not meant to be perfect, it's just in case the
# two makewhatis cron scripts get run close to each other to keep
# them from stepping on each other's toes.  The worst that will
# happen is that they will temporarily corrupt the database...
[ -f $LOCKFILE ] && exit 0
trap "rm -f $LOCKFILE" EXIT
touch $LOCKFILE
makewhatis -w
exit 0

/etc/cron.monthly/
[root@seisyuu cron.monthly]# ls
[root@seisyuu cron.monthly]#

資料:TLXW8のlogrotate初期設定
「本題」に戻る
/etc/logrotate.conf
[root@seisyuu etc]# cat logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# send errors to root
errors root

# 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 lastlog or wtmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    rotate 1
}

# system-specific logs may be configured here

/etc/logrotate.d/apache
[root@seisyuu logrotate.d]# cat apache
/var/log/httpd/access_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/var/log/httpd/agent_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/var/log/httpd/error_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/var/log/httpd/referer_log {
    missingok
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}

/etc/logrotate.d/cron
[root@seisyuu logrotate.d]# cat cron
/var/log/cron {
    missingok
    notifempty
    postrotate
        /bin/kill -HUP `/bin/cat /var/run/crond.pid`
    endscript
}

/etc/logrotate.d/syslog
[root@seisyuu logrotate.d]# cat syslog
/var/log/messages {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/secure {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/maillog {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/spooler {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

/var/log/boot.log {
    postrotate
        /usr/bin/killall -HUP syslogd
    endscript
}

資料:logrotate実行時オプション(「man logrotate」より抜粋)
「本題」に戻る
OPTIONS
-d デバッグモードを有効にする。デバッグモードではログファイルはいじらず、 logrotateのstateファイルもいじらない。一種のシミュレートモード。
-f, --force 強制的にその場でログローテーションさせます。新しい設定を試したり、 古すぎるログファイルを手動で消したい時などに便利です。
-s, --state <statefile> 指定されたstateファイルを使うよう指示します。違うユーザーや、いろんな ログファイルをローテーションさせたりするときに使います。デフォルトのstateファイル は/var/lib/logrotate.statusです。
--usage 使い方を表示してくれます。

資料:logrotate設定ファイルオプション(「man logrotate」より抜粋)
「本題」に戻る
 めんどくさいので英文のまま掲載。実際、英語のままの方がわかりやすい場合も多かったりする。
 logrotate reads everything about the log files it should be handling from the series of configuration files specified on the command line.  Each configuration file can set global options (local definitions override  global  ones,  and  later definitions override earlier ones) and specify a logfile to rotate. A simple configuration file looks like this:

# sample logrotate configuration file
errors sysadmin@my.org
compress
/var/log/messages {
	rotate 5
	weekly
	postrotate
		/sbin/killall -HUP syslogd
endscript
}

"/var/log/httpd/access.log" {
	rotate 5
	mail www@my.org
	errors www@my.org
	size=100k
	postrotate
		/sbin/killall -HUP httpd
	endscript
}

/var/log/news/* {
	monthly
	rotate 2
	missingok
	errors newsadmin@my.org
	postrotate
		kill -HUP `cat /var/run/inn.pid`
	endscript
	nocompress
}

 The  first  few lines set global options; any errors that occur during log file processing are mailed to sysadmin@my.org and logs are compressed after they are rotated.  Note that comments may appear anywhere in the config file  as  long  as the first non-whitespace character on the line is a #.
 The  next section of the config files defined how to handle the log file /var/log/messages. The log will go through five weekly rotations before being removed. After the log file has been rotated (but before the old version of  the  log  has been compressed), the command /sbin/killall -HUP syslogd will be executed.
 The  next  section  defines  the parameters for /var/log/httpd/access.log.  It is rotated whenever is grows over 100k is size, and the old logs files are mailed (uncompressed) to www@my.org after going through 5 rotations, rather then  being removed.  Likewise,  any  errors  that occur while processing the log file are also mailed to www@my.org (overriding the global errors directive).  Note that the double quotes around the filename at  the  beginning  of  this  section  allows logrotate to rotate logs with spaces in the name.
 The last section definest the parameters for all of the files in /var/log/news. Each file is rotated on a monthly basis, and the errors are mailed to newsadmin@my.org. This is considered a single rotation directive and if  errors  occur  for more then one file they are mailed in a single message. In this case, the log files are not compressed.
その他オプション
create mode owner group Immediately after rotation (before the postrotate script is run) the log file is created (with the same name as the log file just rotated). mode specifies the mode for the log file in octal (the same as chmod(2)), owner specifies the user name who will own the log file, and group specifies the group the log file will belong to. Any of the log file attributes may be omitted, in which case those attributes for the new file will use the same values as the original log file for the omitted attributes. This option can be disabled using the nocreate option.
dailyLog files are rotated every day.
delaycompress Postpone compression of the previous log file to the next rotation cycle. This has only effect when used in combination with compress. It can be used when some program can not be told to close its logfile and thus might continue writing to the previous log file for some time.
errors address Any errors that occur during log file processing are mailed to the given address.
extension ext Log files are given the final extension ext after rotation. If compresssion is used, the compression extension(normally .gz) appears after ext.
ifempty Rotate the log file even if it is empty, overiding the notifempty option (ifempty is the default).
include file_or_directory Reads the file given as an argument as if it was included inline where the include directive appears. If a directory is given, most of the files in that directory are read before processing of the including file continues.
 The only files which are ignored are files which are not regular files (such as directories and named pipes) and files whose names end with one of the taboo extensions, as specified by the tabooext directive. The include directive may not appear inside of a log file definition.
mail address When a log is rotated out-of-existence, it is mailed to address. If no mail should be generated by a particular log, the nomail directive may be used.
mailfirst When using the mail command, mail the just-rotated file, instead of the about-to-expire file.
maillast When using the mail command, mail the about-to-expire file, instead of the just-rotated file (this is the default).
missingok If the log file is missing, go on to the next one without issuing an error message. See also nomissingok.
monthly Log files are rotated the first time logrotate is run in a month (this is normally on the first day of the month).
nocompress Old versions of log files are not compressed with gzip. See also compress.
nocopytruncate Do not truncate the original log file in place after creating a copy (this overrides the copytruncate option).
nocreate New log files are not created (this overrides the create option).
nodelaycompress Do not postpone compression of the previous log file to the next rotation cycle (this overrides the delaycompressoption).
nomail Don't mail old log files to any address.
nomissingok If a log file does not exist, issue an error. This is the default.
noolddir Logs are rotated in the same directory the log normally resides in (this overrides the olddir option).
notifempty Do not rotate the log if it is empty (this overrides the ifempty option).
olddir directory Logs are moved into directory for rotation. The directory must be on the same physical device as the log file being rotated. When this option is used all old versions of the log end up in directory. This option may be overriden by the noolddir option.
postrotate/endscript The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside of a log file definition. See prerotate as well.
prerotate/endscript The lines between prerotate and endscript (both of which must appear on lines by themselves) are executed before the log file is rotated. These directives may only appear inside of a log file definition. See postrotate as well.
rotate count Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated.
size size Log files are rotated when they grow bigger then size bytes. If size is followed by M, the size if assumed to be in megabytes. If the k is used, the size is in kilobytes. So size 100, size 100k, and size 100M are all valid.
tabooext [+] list The current taboo extension list is changed (see the include directive for information on the taboo extensions). If a + precedes the list of extensions, the current taboo extension list is augmented, otherwise it is replaced. At startup, the taboo extension list contains .rpmorig, .rpmsave, ,v and ~.
weekly Log files are rotated if the current weekday is less then the weekday of the last rotation or if more then a week has passed since the last rotation. This is normally the same as rotating logs on the first day of the week, but it works better if logrotate is not run every night.


目次に戻る

後書き或いは感想

 つ、疲れた・・・・・・。
 何つっても、大量のcronやらlogrotateの設定ファイル、manページを整理し直すことになったわけで。疲れた。
 実際、このページは実に二週間に渡って延々と(だらだらと)書き続けていた。
 もう、はっきり言ってlogrotateとかcronとかは見る気なし。しばらくの間は。

 本当は、logrotateのmanからの抜粋も独自に日本語訳しようかとも思ったんだけど、いい加減面倒になったし、私の下手な日本語訳よりは、素直な英文のまま上げさせてもらいました。turboさん、logorotateの作者さん、ごめんなさい。
 ログローテーションはシステムの規模が大きくなるほど、しっかりと作り込む必要のある部分です。かといって、logrotate自体は小さくまとまっていて、設定ファイルを作り込むことによって徐々に機能を強化していくといったなんだか可愛らしいプログラムです。cronと簡単に組み合わせられるのも良いですね。
 というわけで、ワークステーションユーザーの方もこれを機にcronとかlogrotateとか眺めてみてください。勉強になります。結局参考資料はmanページだけだったし。
目次に戻る

トップぺージ>>>「いろいろ」目次