Gentooのシャットダウン時に自動でPostgreSQLを終了してくれなくて毎回
「pg_ctl: Another postmaster may be running. Trying to start postmaster anyway.」
と表示され面倒くさいので何とかしようということで、/etc/inittabについて調べたのでメモ。
inittabを見ると
l0:0:wait:/sbin/rc shutdown
と書かれている。
つまりシャットダウン時/sbin/rc shutdownが実行される。
このときpostgreSQLの終了処理が行われず、その結果/usr/local/pgsql/data/postmaster.pidが取り残されたままになるため、Gentoo起動時に面倒なメッセージが出力されるということ。
実際のGentoo終了時のメッセージを見てみると、
The system is going down for system halt NOW!
INIT: Switching to runlevel: 0
INIT: Sending processes the TERM signal
* Stopping local …
* Stopping vixie-cron …
* Stopping… …
…
…
(省略)
と表示されている。
/etc/init.d/postgresを見ると終了時の処理に
echo -n “Stopping PostgreSQL: ”
という記述があるがGentoo終了時にそのようなメッセージは表示されていないため/etc/init.d/postgres stopは実行されていないと思われる。
ためしに/etc/inittabの最後に、
Po:0:wait:/etc/init.d/postgres stop
と記述してみたが、やはりPostgreSQL終了コマンドは実行されない。
次に、
ランレベル0についての記述
l0:0:wait:/sbin/rc shutdown
の前に
Po:0:wait:/etc/init.d/postgres stop
を書いてみた。
つまり、
Po:0:wait:/etc/init.d/postgres stop
l0:0:wait:/sbin/rc shutdown
この設定でGentooを終了したところ、PostgreSQLの終了処理が実行された。
/etc/inittabも上から順番に実行されるようだ。
しかし、/etc/init.d/postgresはちゃんと/etc/runlevels/defaultに登録されているのに、ランレベルdefaultの終了時に実行されないのが腑に落ちない。
そこで/etc/init.d/postgresを見てみると…
#! /bin/sh
…
(省略)
/bin/shで書かれている。inittabで制御されるためには/sbin/runscriptを使いinitスクリプトとして書かれなければならない。どうやらこれが原因のようだ。
ということで、今ある/etc/init.d/postgresを/etc/init.d/postgres.shとリネームした後、initスクリプトとしての/etc/init.d/postgresを作成しそこからpostgres.shを呼ぶようにしてみる。
/etc/init.d/ディレクトリ内においてviコマンドで新規ファイルを作成すると勝手にinitスクリプトのフォーマットを書いてくれるのにはちょっと驚いた。
とりあえず、簡単に以下のようなinitスクリプトを書いてみた。
$ mv postgres postgres.sh
$ vi postgres
#!/sbin/runscript # Copyright 1999-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: $ depend() { after checkroot } start() { [ -e /etc/init.d/postgres.sh ] && { /etc/init.d/postgres.sh start; } } stop() { [ -e /etc/init.d/postgres.sh ] && { /etc/init.d/postgres.sh stop; } } restart() { [ -e /etc/init.d/postgres.sh ] && { /etc/init.d/postgres.sh stop; } }
$ chmod a+x postgres
これでGentoo終了時にPostgreSQLを終了してくれる。
[参考]
第10回 Linux起動の仕組みを理解しよう[init/inittab編]
Gentoo Linux ドキュメント — initスクリプト