MavericksにSupervisorをインストールしてみた
Mac上でdaemontoolsみたいなことをやる君欲しかったので、使ったことないけど以前から気になっていたSupervisorを試してみました。SupervisorはPython製のプロセス管理ツールです。結論から言うとroot権限が必要ないしconfも書きやすいのでオススメです。
Pythonインストール
homebrewからインストールしましょう。
$ brew install python
pip
Pythonインストール後の画面に表示されるようにsetuptoolsとpipをupgradeします。 (参考:https://github.com/Homebrew/homebrew/wiki/Homebrew-and-Python)
$ pip install --upgrade setuptools $ pip install --upgrade pip
Supervisorインストール
pipで一撃です。公式ドキュメントによるとpipのバージョンが1.4以上は--preオプションつけてね、と記述があるので追記しています。
$ pip install supervisor --pre
supervisord.conf
設定ファイルを用意します。
$ mkdir -p /usr/local/share/supervisor/conf.d $ cd /usr/local/share/supervisor $ echo_supervisord_conf > supervisord.conf
作成した設定ファイルはほぼデフォルトでいいのですが一箇所だけ修正します。 supervisord.conf138行目あたりです。
[include] - files = /etc/supervisord.d/*.ini + files = /usr/local/share/supervisor/conf.d/*.conf
管理下に置きたいプログラムの設定ファイルの作成
Supervisorで管理したいプログラムの設定を作成します。 hubotでサンプル書いてみます。
$ touch /usr/local/share/supervisor/conf.d/hubot.conf
作成したhubot.confに下記の設定を記述します。 hubotの注意点としてnpmが動くようにPATHを通してやる必要がありました。 登録するプログラムに応じてPATHの見直しをしてください。
[program:hubot] command=/Users/mitz/work/myhubot/bin/hubot -a irc --name hubot directory=/Users/mitz/work/myhubot user=mitz numprocs=1 stdout_logfile=/tmp/hubot_out.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=3 stderr_logfile=/tmp/hubot_err.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=3 autostart=false autorestart=true environment=PATH="/usr/local/bin:$PATH",HOME="/Users/mitz/work/myhubot", \ USER="mitz",HUBOT_IRC_SERVER="irc.example.local",HUBOT_IRC_ROOMS="#mitz", \ HUBOT_IRC_NICK="hubot",HUBOT_IRC_UNFLOOD="true"
起動確認
Supervisorが動くかどうか試してみましょう。
$ supervisord -c supervisord.conf $ supervisorctl start hubot
supervisordをlaunchdに登録
supervisordをマシン起動時にプロセスが立ち上がるようにlaunchdに登録します。
$ touch ~/Library/LaunchAgents/com.agendaless.supervisord.plist
com.agendaless.supervisord.plist に下記を記述してください。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>KeepAlive</key> <dict> <key>SuccessfulExit</key> <false/> </dict> <key>Label</key> <string>com.agendaless.supervisord</string> <key>ProgramArguments</key> <array> <string>/usr/local/bin/supervisord</string> <string>-n</string> <string>-c</string> <string>/usr/local/share/supervisor/supervisord.conf</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
これでlaunchctlでsupervisordが動くようになりました。
$ launchctl load ~/Library/LaunchAgents/com.agendaless.supervisord.plist
それではよきSupervisorライフをお送りください!