50.自作サービスをchkconfigに登録する方法
ランレベル毎にサービス起動の設定ができるchkconfigコマンドに自作のサービスを登録してchkconfigコマンドで設定できるようにする方法
まずは登録したいシェルを作成します。
例)testシェル
#!/bin/sh
#
# /etc/rc.d/init.d/test
#
# Starts the test daemon(test version)
#
# chkconfig: 345 44 56
# description: test
case $1 in
start)
echo "test service start!"
;;
stop)
echo "test service stop!"
;;
status)
echo "test service status!"
;;
*)
echo "Usage: test [start|stop|status]"
;;
esac
exit 0 |
シェルはサンプルなので特に実行されるものはありません。
ここでのポイントは赤字部分
chkconfig: AAA BBB CCC
AAA ・・・chkconfigでonとなるランレベル
BBB ・・・startの番号
CCC ・・・stopの番号
description: 説明
必ずこの「chkconfig: 」と「description: 」を記述しなければchkconfigに登録できないので注意が必要です。
上記の例だと。。。ランレベル3、4、5の時にon、それ以外はoff、startの番号は44番でstopの番号は56番
このシェルを/etc/init.dディレクトリへコピーして
# chkconfig --add test
# chkconfig --list | grep test
test 0:off 1:off 2:off 3:on 4:on 5:on 6:off
# cd /etc/rc.d
# ls -l rc[0-6].d/*test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc0.d/K56test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc1.d/K56test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc2.d/K56test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc3.d/S44test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc4.d/S44test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc5.d/S44test -> ../init.d/test
lrwxrwxrwx 1 root root 14 2000-00-00 00:00 rc6.d/K56test -> ../init.d/test |
それぞれのランレベルに指定した番号で登録されたことが確認できます。
chkconfigに登録されたので、serviceコマンドで実行できるか確認します
# service test start
test service start!
# service test stop
test service stop!
# service test status
test service status!
# service test
Usage: test [start|stop|status] |
chkconfigの登録から削除する場合は次のようにします。
# chkconfig --del test
# chkconfig --list | grep test |
健闘を祈るだワン!
|