mt-daapd を自動的に起動/停止させよう!

ガチャさま

mt-daapd をインストールしたのだけど、/etc/init.d に置いておくファイルをどうすれば良いのか困ってた。で、どうも debian の /etc/init.d に置くファイルの雛形が /etc/init.d にあるらしいので確認してみた。

paraches@debian:/etc/init.d$ cat skeleton
#! /bin/sh
#
# skeleton      Example initscript
#               This file should be used to construct scripts to be
#               placed in /etc/init.d.
#
# Author:       Miquel van Smoorenburg <miquels@cistron.nl>.
#               Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
#               Please remove the "Author" lines above and replace them
#               with your own name if you copy and modify this script.
#
# Version:      @(#)skeleton  2.85-23  28-Jul-2004  miquels@cistron.nl
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="some daemon"
NAME=daemon
DAEMON=/usr/sbin/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Read config file if it is present.
#if [ -r /etc/default/$NAME ]
#then
#       . /etc/default/$NAME
#fi

#
#       Function that starts the daemon/service.
#
d_start() {
        start-stop-daemon --start --quiet --pidfile $PIDFILE ?
                --exec $DAEMON
}

#
#       Function that stops the daemon/service.
#
d_stop() {
        start-stop-daemon --stop --quiet --pidfile $PIDFILE ?
                --name $NAME
}

#
#       Function that sends a SIGHUP to the daemon/service.
#
d_reload() {
        start-stop-daemon --stop --quiet --pidfile $PIDFILE ?
                --name $NAME --signal 1
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        d_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        d_stop
        echo "."
        ;;
  #reload)
        #
        #       If the daemon can reload its configuration without
        #       restarting (for example, when it is sent a SIGHUP),
        #       then implement that here.
        #
        #       If the daemon responds to changes in its config file
        #       directly anyway, make this an "exit 0".
        #
        # echo -n "Reloading $DESC configuration..."
        # d_reload
        # echo "done."
  #;;
  restart|force-reload)
        #
        #       If the "reload" option is implemented, move the "force-reload"
        #       option to the "reload" entry above. If not, "force-reload" is
        #       just the same as "restart".
        #
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 1
        d_start
        echo "."
        ;;
  *)
        # echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
paraches@debian:/etc/init.d$ 

なるほど〜 start-stop-daemonっていうので起動、停止をしているのね。で、最初の数行書き換えれば後はこのまま行けそうじゃない?楽チン楽チン!
というわけで、/etc/init.d/skeleton の最初の方を以下の様に書き換えた。

DESC="mt-daapd daemon"
NAME=mt-daapd
DAEMON=/usr/local/sbin/$NAME

これで mt-daapd を動作するようにすれば、起動やら停止の色々が行われるようになるはず!
さっそく確認してみる。

paraches@debian:/etc/init.d$ sudo chmod 755 mt-daapd
paraches@debian:/etc/init.d$ sudo ./mt-daapd start
Starting mt-daapd daemon: mt-daapd.
paraches@debian:/etc/init.d$ ps ax | grep mt-daapd
23094 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23095 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23096 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23097 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23098 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23100 pts/0    R+     0:00 grep mt-daapd
paraches@debian:/etc/init.d$ 

問題なく起動した! でも、なんで5つも???
ま、難しい事は置いておいて、次は停止の確認。

paraches@debian:/etc/init.d$ sudo ./mt-daapd stop
Stopping mt-daapd daemon: mt-daapd.
paraches@debian:/etc/init.d$ ps ax | grep mt-daapd
23094 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23095 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23096 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23097 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23098 pts/2    S      0:00 /usr/local/sbin/mt-daapd
23100 pts/0    R+     0:00 grep mt-daapd
paraches@debian:/etc/init.d$ 

あれ? 止まらない? なぜ? start-stop-daemon の --stop はシグナルを送るってなってるけど、その場合のシグナルはどうしたら良いんだろう? skeleton のまんまだと何も送ってないんでない? 違うのかな?
と、困ってしまったので、mt-daapd に最初から付いてきてる mt-daapd で動作を確認してみる。

paraches@debian:~/myTest/mt-daapd-0.2.4/contrib$ sudo ./mt-daapd stop
./mt-daapd: line 10: /etc/init.d/functions: そのようなファイルやディレクトリはありません
Shutting down DAAP server: 
paraches@debian:~/myTest/mt-daapd-0.2.4/contrib$ ps ax | grep mt-daapd
23091 pts/0    R+     0:00 grep mt-daapd
paraches@debian:~/myTest/mt-daapd-0.2.4/contrib$ 

何やらエラーは出てるけど、mt-daapd は停止しているぞ! あのエラーは何? というわけで mt-daapd のエラーの場所と停止をしてる場所を確認してみた。

# source function library
. /etc/init.d/functions
[ -e /etc/daapd.conf ]
(省略)
stop() {
        echo -n $"Shutting down DAAP server: "
        # This is broken.
        killall -INT mt-daapd
#       killproc mt-daapd
        RETVAL=$?

        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mt-daapd
}

う〜ん、エラーは/etc/init.d/functions が無いからか〜。でも、それは何? /etc/daapd.conf と関係あり? 設定ファイルを読み込む何かか?
ま、それは無視して停止はどうやってるのかな…? あぁ、killall -INT mt-daapd で停止なんだ。
そっか、それじゃこれをそのまま skeleton で作った mt-daapd にコピーしちゃえば良いね。

d_stop() {
		killall -INT mt-daapd
#        start-stop-daemon --stop --pidfile $PIDFILE --name $NAME
}

さっそく新しい mt-daapd で停止も試してみよう。

paraches@debian:/etc/init.d$ sudo ./mt-daapd stop
Stopping mt-daapd daemon: mt-daapd.
paraches@debian:/etc/init.d$ ps ax | grep mt-daapd
23110 pts/0    R+     0:00 grep mt-daapd
paraches@debian:/etc/init.d$ 

ん! ちゃんと停止できてる。とりあえず動くって状態だけど、ま、これで良しとしよう。


次はサーバの起動時や停止時に自動でスクリプトが起動するように /etc/rcX.d にリンクを張らないといけない。けど、これは update-rc.d という便利なコマンドが用意されている。

paraches@debian:/etc$ sudo update-rc.d mt-daapd defaults
 Adding system startup for /etc/init.d/mt-daapd ...
   /etc/rc0.d/K20mt-daapd -> ../init.d/mt-daapd
   /etc/rc1.d/K20mt-daapd -> ../init.d/mt-daapd
   /etc/rc6.d/K20mt-daapd -> ../init.d/mt-daapd
   /etc/rc2.d/S20mt-daapd -> ../init.d/mt-daapd
   /etc/rc3.d/S20mt-daapd -> ../init.d/mt-daapd
   /etc/rc4.d/S20mt-daapd -> ../init.d/mt-daapd
   /etc/rc5.d/S20mt-daapd -> ../init.d/mt-daapd
paraches@debian:/etc$ 

とりあえず default でやったら起動も停止も 20 番だった。こういうもんなの?ま、いっか。


というわけで、ちゃんとシステム起動時に自動で mt-daapd も起動するようになった! めでたし、めでたし。