1、需求
现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断。当进程中断的时候我希望能自动重新启动它,此时,就需要使用到了Supervisor。Supervisor起到守护进程的作用。
2、安装
https://pypi.python.org/pypi/supervisor
tar zxvf supervisor-3.0.tar.gz cd supervisor-3.0 python2.7 setup.py build python2.7 setup.py install
需要依赖于meld3-1.0.0.tar.gz,联网安装,由于所在机器无法上网,故进行手动安装meld。
参照上面方法再进行安装meld,安装完成之后再次进行install即可。
测试安装是否成功:echo_supervisord_conf
配置文件:echo_supervisord_conf > /etc/supervisord.conf
3、Supervisor相关
supervisord : supervisor的服务器端部分,启动supervisor就是运行这个命令
supervisorctl:启动supervisor的命令行窗口
4、Demo1测试
需求是对本地的API进程进行守护,要求这个服务能在意外停止后自动重启。
API的执行命令为:python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005
修改配置文件:
[program:mysqlapi] command = python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 autostart = true autorestart = true startsecs = 3 stdout_logfile = /data1/guosong/mysqlapi/log/mysqlapi_demo1.log
启动supervisord:
[root@typhoeus79 mysqlapi]# ps aux |grep super root 15377 0.0 0.0 61268 788 pts/3 R+ 16:23 0:00 grep super [root@typhoeus79 mysqlapi]# supervisord Unlinking stale socket /var/tmp/supervisor.sock [root@typhoeus79 mysqlapi]# ps aux |grep super root 15458 0.0 0.0 147148 7976 ? Ss 16:23 0:00 /usr/bin/python /usr/bin/supervisord root 15533 0.0 0.0 61268 792 pts/3 S+ 16:23 0:00 grep super
查看进程状态:
[root@typhoeus79 mysqlapi]# supervisorctl mysqlapi RUNNING pid 15460, uptime 0:01:19 supervisor> status mysqlapi RUNNING pid 15460, uptime 0:01:21 supervisor> exit [root@typhoeus79 mysqlapi]# ps aux |grep 8005 root 15460 0.2 0.0 208436 15484 ? S 16:23 0:00 python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 root 16314 0.0 0.0 61268 788 pts/3 S+ 16:24 0:00 grep 8005 [root@typhoeus79 mysqlapi]#
kill测试:
[root@typhoeus79 mysqlapi]# ps aux |grep 8005 root 15460 0.2 0.0 208436 15484 ? S 16:23 0:00 python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 root 16314 0.0 0.0 61268 788 pts/3 S+ 16:24 0:00 grep 8005 [root@typhoeus79 mysqlapi]# [root@typhoeus79 mysqlapi]# kill 15460 [root@typhoeus79 mysqlapi]# ps aux |grep 8005 root 17431 21.0 0.0 208436 15484 ? S 16:25 0:00 python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 root 17437 0.0 0.0 61268 772 pts/3 R+ 16:25 0:00 grep 8005 [root@typhoeus79 mysqlapi]# kill -9 17431 [root@typhoeus79 mysqlapi]# ps aux |grep 8005 |grep -v grep root 17540 7.0 0.0 208440 15488 ? S 16:25 0:00 python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 [root@typhoeus79 mysqlapi]#
使用kill以及kill -9 进程的id都发生变化
使用supervisorctl进行程序重启:
[root@typhoeus79 mysqlapi]# supervisorctl stop all mysqlapi: stopped [root@typhoeus79 mysqlapi]# ps aux |grep 8005 |grep -v grep [root@typhoeus79 mysqlapi]# supervisorctl start all mysqlapi: started [root@typhoeus79 mysqlapi]# ps aux |grep 8005 |grep -v grep root 19649 3.0 0.0 208440 15488 ? S 16:28 0:00 python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005 [root@typhoeus79 mysqlapi]#
5、Demo2测试
守护多个进程,修改/etc/supervisord.conf配置
[program:mysqlapi] command=python26 /data1/guosong/mysqlapi/main/api_main.py --port=140%(process_num)02d process_name=%(program_name)s_%(process_num)02d ; process_name expr (default %(program_name)s) numprocs=8 numprocs_start=81 startretries=3 stopwaitsecs=10 autorstart=true log_stdout=true log_stderr=true logfile=/data1/guosong/mysqlapi/log/mysql_api_demo.log
超过2位数如何表示?
command=python26 /data1/guosong/mysqlapi/main/api_main.py --port=140%(process_num)02d
结果演示:
6、问题集锦
问题1-mysqlapi entered FATAL state, too many start retries too quickly:
2014-05-15 17:26:30,898 INFO exited: mysqlapi (exit status 1; not expected) 2014-05-15 17:26:30,899 INFO received SIGCLD indicating a child quit 2014-05-15 17:26:31,901 INFO gave up: mysqlapi entered FATAL state, too many start retries too quickly
原因在于版本导致,api的脚本版本为python26,故使用supervisord也需要使用python26进行安装,查看进程信息如下:
root 24058 0.0 0.0 151420 10204 ? Ss 18:28 0:00 /usr/bin/python26 /usr/bin/supervisord -c /etc/supervisord.conf
问题2- http://127.0.0.1:9001 refused connection:
[root@typhoeus79 20140515]# supervisorctl http://10.75.xxx.xxx:9001 refused connection
原因在于没有开启inet_http_server,配置文件默认使用分号注释每行
将inet_http_server前面的分号去除后,重新开启supervisord
[root@typhoeus79 20140515]# /etc/init.d/supervisord stop Stopping supervisord: [ OK ] [root@typhoeus79 20140515]# supervisord -c /etc/supervisord.conf [root@typhoeus79 20140515]# supervisorctl mysqlapi:mysqlapi_81 RUNNING pid 32513, uptime 0:00:02 mysqlapi:mysqlapi_82 RUNNING pid 32511, uptime 0:00:02 mysqlapi:mysqlapi_83 RUNNING pid 32512, uptime 0:00:02 mysqlapi:mysqlapi_84 RUNNING pid 32509, uptime 0:00:02 mysqlapi:mysqlapi_85 RUNNING pid 32510, uptime 0:00:02 mysqlapi:mysqlapi_86 RUNNING pid 32507, uptime 0:00:02 mysqlapi:mysqlapi_87 RUNNING pid 32508, uptime 0:00:02 mysqlapi:mysqlapi_88 RUNNING pid 32514, uptime 0:00:02
访问界面: