前回のシステムではrc.localにスクリプトを書いて起動していたのですが、これだと終了時に自動処理をさせることができきないのと、ランレベルに応じた処理ができません。それにinsservとかを使って登録できる方がなんとなく「システム管理」をしている感じがします。
とりあえず最小限のスクリプトを先達の雛形に添って作成。ktdというのはこれから作成する予定のデーモンの名前です。実態は何もしない無限ループです。
#! /bin/sh ### BEGIN INIT INFO # Provides: ktd - kawacchi timer daemon # Required-Start: $all # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: daemon for detect gpio rise and report via unix domain socket ### END INIT INFO case "$1" in start) echo "Starting ktd" /home/pi/kt/ktd/ktd & ;; stop) echo "Stopping ktd" killall ktd ;; *) echo "Usage: /etc/init.d/ktd start|stop" exit 1 ;; esac exit 0
直接実行してみるとroot権限で実行されました。
pi@raspberrypi:~/kt/ktd $ sudo /etc/init.d/ktd start Starting ktd pi@raspberrypi:~/kt/ktd $ ps au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 634 0.0 0.1 4052 1740 tty1 Ss+ 06:58 0:00 /sbin/agetty --noclear tty1 linux root 635 0.0 0.2 3872 1928 ? Ss+ 06:58 0:00 /sbin/agetty --keep-baud 115200 38400 9600 pi 703 0.0 0.4 6244 4472 pts/0 Ss 07:02 0:01 -bash root 1214 0.3 0.1 10388 1204 pts/0 Sl 07:35 0:00 /home/pi/kt/ktd/ktd pi 1223 0.0 0.2 4740 2064 pts/0 R+ 07:35 0:00 ps au
最初はrc*.dの何処にもスクリプトが入っていませんが、insservを実行するとリンクが登録されました。Required-startが反映されたようで実行順位が04になっています。
pi@raspberrypi:~/kt/ktd $ ls -l /etc/rc*.d/*ktd ls: cannot access /etc/rc*.d/*ktd: No such file or directory pi@raspberrypi:~/kt/ktd $ sudo insserv -d ktd pi@raspberrypi:~/kt/ktd $ ls -l /etc/rc*.d/*ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc0.d/K01ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc1.d/K01ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc2.d/S04ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc3.d/S04ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc4.d/S04ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc5.d/S04ktd -> ../init.d/ktd lrwxrwxrwx 1 root root 13 Feb 12 08:18 /etc/rc6.d/K01ktd -> ../init.d/ktd pi@raspberrypi:~/kt/ktd $ sudo insserv -r ktd pi@raspberrypi:~/kt/ktd $ ls -l /etc/rc*.d/*ktd ls: cannot access /etc/rc*.d/*ktd: No such file or directory
削除も簡単にできるようです。もう1回登録をして再起動するとこんな感じでした。ちゃんと起動していますね。
pi@raspberrypi:~ $ ps axu | grep ktd root 350 0.0 0.1 10388 1316 ? Sl 08:23 0:00 /home/pi/kt/ktd/ktd pi 725 0.0 0.2 4276 1944 pts/0 S+ 08:25 0:00 grep --color=auto ktd
次はstart-stop-daemonあたりを使用して、もっとそれっぽくしてみたいと思います。





納得したらすぐにシェア!