2012年7月3日

使用supervisord管理程序-在virtualenv啓動tornado, celeryd

當我們在server上裝了一堆服務之後,程序管理就變得很重要

無意間發現supervisor這個好用的process management system

我們先試著控制tornado web server以及celery task management system

首先進入virtualenv環境 安裝supervisor

sudo pip install supervisor
supervisor最重要的就是他的設定檔supervisord.conf

預設是放在/etc/supervisord.conf

執行時也會檢查執行路徑裡面的supervisord.conf或用 "-c" 指定路徑

吐出範例檔

sudo echo_supervisord_conf > supervisord.conf
其中inet_http_server是管理程序狀態的網頁界面

確認logfile和pidfile路徑存在且可寫入,並且和/etc/init.d/supervisord裡設置相同

設定nodaemon=true 因為我們希望在背景執行

設定celery
[program:celery] command=celeryd --loglevel=INFO ; Set PYTHONPATH to the directory containing celeryconfig.py environment=PYTHONPATH="/home/sushiwen/myproject:/home/sushiwen/myproject/venv/lib/python2.7:/home/sushiwen/myproject/venv/lib/python2.7/site-packages:$PYTHONPATH" directory=/home/sushiwen/myproject user=sushiwen numprocs=1 stdout_logfile=/var/log/celery/celeryd.log stderr_logfile=/var/log/celery/celeryd.log autostart=true autorestart=true startsecs=10 ; Need to wait for currently executing tasks to finish at shutdown. ; Increase this if you have very long running tasks. stopwaitsecs = 600 ; if rabbitmq is supervised, set its priority higher ; so it starts first priority=998


設定tornado
[program:myproject] environment=PYTHONPATH="/home/sushiwen/myproject:/home/sushiwen/myproject/venv/lib/python2.7:/home/sushiwen/myproject/venv/lib/python2.7/site-packages:$PYTHONPATH" directory=/home/sushiwen/myproject command=sh runvenv venv python main.py --port=8090 autostart=true autorestart=true redirect_stderr=true stdout_logfile = /var/log/tornado/tornado.log

其中runvenv為執行virtualenv的shell script,其用意和上面environment設置有部份重複

設定完成加入開機執行
sudo update-rc.d supervisord defaults Centos curl https://raw.github.com/gist/176149/88d0d68c4af22a7474ad1d011659ea2d27e35b8d/supervisord.sh sudo chmod +x supervisord.sh sudo mv supervisord.sh /etc/supervisord.sh chkconfig --add supervisord chkconfig supervisord --level 345 on