supervisor的优点:
1:管理你的worker进程,让你的进程随机器启动而启动,当某个worker宕掉时自动重启该worker
2:可以配置指定worker的启动个数
3:supervisor管理的进程必须是非守护进程。
4:采用kill -9强制杀掉supervisord进程,那么supervisor管理的worker将由init进程管理,并继续运行,不会关闭,并且能正常服务于client的请求。
5:采用kill杀掉supervisord进程,那么supervisor管理的所有worker将关闭
自己开发的应用往往也希望做到随系统自动启动, 而且启动之后最好还能方便的控制其停止/重启. 传统的做法是在 /etc/init.d/
下建立启动脚本, 但这个方法非常繁琐, 容易出错, 而且不同服务器/不同版本的配置又有差异,所以可以采用supervisord用于管理和监控进程。
1:先安装setuptools:
>wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086
>sh setuptools-0.6c11-py2.6.egg
2:再安装suoervisor
>essy_install supervisor
3:默认安装在/usr/bin/目录下:
/usr/bin/echo_supervisord_conf
/usr/bin/supervisorctl
/usr/bin/supervisord
4:将supervisord_conf改成配置文件
>cp echo_supervisord_conf supervisord.conf
5:开启supervisor http可视化查看所监控的进程的状态,修改supervisord.conf配置文件,添加如下,默认是没有的:
[inet_http_server]
port=127.0.0.1:9001
6:监控举例,这里修改supervisord.conf配置文件,添加如下配置
[program:Gearman_Reverse_Worker] #Gearman_Reverse_Worker为进程取个名字
command=/usr/local/php5/bin/php Reverse.php #启动该进程需要的命令
directory=/usr/local/nginx1.0/www #启动该进程之前需要跳转到相应的目录
autostart=true #随supervisord启动而启动
autorestart=true #如果该进程出现故障将自动重启
exitcodes=0 #正常退出码,supervisord将不重启该进程
stopsignal=KILL #杀死进程的信号,将不重启,待验证
redirect_stderr=true #重定向stderr到stdout
numprocs=10 #启动的进程个数
process_name=%(program_name)s_%(process_num)02d #当numprocs大于1时,指定进程名的格式
7:启动supervisord
>cd /usr/bin
>./supervisord
8:验证是否启动了Reverse
>ps aux | grep Reverse
root 2885 1.2 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2886 0.8 0.4 30780 4788 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2887 0.9 0.4 30780 4788 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2888 0.8 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2889 0.6 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2890 0.5 0.4 30780 4800 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2891 0.6 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2892 1.1 0.4 30780 4788 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2893 0.8 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php
root 2894 0.7 0.4 30780 4792 ? S 00:43 0:00 /usr/local/php5/bin/php Reverse.php