2012年12月23日日曜日

daemontools,使ってますか?(中編)

murayです.23日も終わりに近づいていますが,daemontools使ってますか?(中編)です.

さっそくですが,daemontoolsをインストールしてみましょう.

とりあえずはこのページの通りに,現時点での最新版を落として展開します.
# sudo -i
# curl -O http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
# tar zxvf daemontools-0.76.tar.gz
# cd admin/daemontools-0.76

手順に従ってインストールしてみます.

# ./package/install
Linking ./src/* into ./compile...
Compiling everything in ./compile...
./load envdir unix.a byte.a 
/usr/bin/ld: errno: TLS definition in /lib64/libc.so.6 section .tbss mismatches non-TLS reference in envdir.o
/lib64/libc.so.6: could not read symbols: Bad value
collect2: ld はステータス 1 で終了しました
make: *** [envdir] エラー 1
ダメでした...現在のdaemontoolsは,インストールする前にパッチを当てる必要があります.パッチはここにあります.

# curl -O http://djbware.csi.hu/patches/daemontools-0.76.errno.patch
# patch -p1 < daemontools-0.76.errno.patch
patching file src/error.h

再挑戦します.

# ./package/install
...
Copying commands into ./command...
Creating symlink daemontools -> daemontools-0.76...
Making command links in /command...
Making compatibility links in /usr/local/bin...
Creating /service...
Adding svscanboot to inittab...
init should start svscan now.

今度は成功しました.

さて,本来はこの時点で/etc/inittabにsvscanbootというコマンドが実行されるように書き込まれ,svscanというプログラムが実行されているはずなのですが,なぜか私の環境では(再起動しても)動きませんでした.
そこで,svscanの起動を直接制御できるよう,/etc/init.d/以下に,起動スクリプトを作成しました.

#!/bin/sh
. /etc/init.d/functions

PATH=/usr/local/bin:$PATH

start() {
  if [ -e /var/lock/subsys/svscan ]; then
    action "svscan lockfile exists /var/lock/subsys/svscan: " /bin/false
  else
    action "Starting svscan: " /bin/true
    touch /var/lock/subsys/svscan
    exec env - PATH="$PATH" \
    sh -c 'svscan /service & echo $! > /var/run/svscan.pid'
  fi
}

stop_each_svc() {
  if ls /service/* > /dev/null 2>&1; then
    for s in /service/*; do
      svc -dx $s
      action "Stopping $s: " /bin/true
      if [ -e $s/log ]; then
        svc -dx $s/log
        action "Stopping $s/log" /bin/true
      fi
    done
  fi
}

stop() {
  if [ -f /var/run/svscan.pid ]; then
    kill `cat /var/run/svscan.pid`
    stop_each_svc

    rm -f /var/run/svscan.pid
    rm -f /var/lock/subsys/svscan
    action "Stopping svscan: " /bin/true
  fi
}


case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0
さて,実際にサービスをdaemontools管理下に入れてみます.動かしたいサービスのディレクトリ等に,serviceというディレクトリを作成し,runという名前の実行可能スクリプトを配置します.このスクリプトにはサービスの起動に必要な処理を記述します.

$ sudo su - bot
$ mkdir some_bot/service
$ vi some_bot/service/run
#!/bin/sh

cd /home/bot/some_bot # サービスディレクトリに移動
exec ./bin/bot.pl 2>&1 # サービスを起動するコマンド

$ chmod +x some_bot/service/run
このserviceディレクトリが,daemontoolsにとっての1つの管理対象になります.runスクリプトを作成したら,/serviceディレクトリに,シンボリックリンクを作成します.

$ sudo ln -s /home/bot/some_bot/service /service

svscanは5秒に1度/serviceを参照するため,これで5秒以内にはrunスクリプトが実行され,サービスが起動されます.

サービスの起動,停止,再起動は,svcコマンドで制御することができます.

$ svc -u some_bot/service # 起動
$ svc -d some_bot/service # 停止
$ svc -t some_bot/service # 再起動

23日の終わりが迫ってきてしまったので,今日はここまで...今回で終わりの予定でしたが,まだいくつか話したいことがあるのでadvent calendar終了後に最終編を書きたいと思います.

18 件のコメント:

  1. Thanks for sharing, great post!

    Áo khoác Đà Lạt chuyên sỉ và lẻ áo khoác aokhoacdalat.com với giá ưu đãi nhất 2020. Luôn cập nhật mẫu mới chất lượng và dày ấm nhất kể cả với thời tiết lạnh giá các nước có du học sinh và xuất khẩu lao động. Aokhoacdalat.com được đồng hành cùng các bạn!!!!

    返信削除
  2. This is incredibly charming substance! I have taken a lot of joy 일본야동

    返信削除
  3. This post is good enough to make somebody understand this amazing thingngs 일본야동 target=

    返信削除
  4. Love this blog!!!Thanks a lot for sharing this with all folks you actually read my mind Definitely believe that what you said. 국산야동

    返信削除
  5. Thanks for sharing this marvelous post. I m very pleased to read this article. 일본야동

    返信削除

  6. You have touched some pleasant factors here. Any way keep up wrinting. 국산야동

    返信削除
  7. I was impressed by your writing. Your writing is impressive. I want to write like you.온라인바둑이
    ‮‬

    返信削除
  8. 온라인카지노
    I’m not sure where you are getting your info, however great topic.

    返信削除
  9. Wonderful work! 더킹카지노
    This is the type of info that are meant to be shared across the web.

    返信削除
  10. Nice one! Thank you for sharing this post. Your blog posts are more 토토 interesting and impressive.

    返信削除
  11. What’s up to all, it’s genuinely a fastidious for me to visit this website, it consists of priceless Information. 메이저사이트

    返信削除
  12. This is very interesting, You are a very skilled blogger. 경마사이트

    返信削除
  13. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers Feel free to visit my website; 바카라사이트

    返信削除
  14. Whats Taking place i’m new to this, I stumbled upon this I’ve discovered It positively helpful and it has aided me out loads. 토토

    返信削除
  15. Nice Blog. Thanks for sharing with us. Such amazing information.
    파워볼게임

    返信削除
  16. Thanks for sharing with us. Such amazing information. Your blog posts are more interesting and impressive. I simply couldn't leave your site just my reading this blog. I am really impressed with the content and way of writing. Keep sharing more.
    protective order virginia
    code of virginia emergency protective order

    返信削除