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 :
[iyunv@typhoeus79 mysqlapi]# ps aux |grep super
root 15377 0.0 0.0 61268 788 pts/3 R+ 16:23 0:00 grep super
[iyunv@typhoeus79 mysqlapi]# supervisord
Unlinking stale socket /var/tmp/supervisor.sock
[iyunv@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
查看进程状态 :
[iyunv@typhoeus79 mysqlapi]# supervisorctl
mysqlapi RUNNING pid 15460, uptime 0:01:19
supervisor> status
mysqlapi RUNNING pid 15460, uptime 0:01:21
supervisor> exit
[iyunv@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
[iyunv@typhoeus79 mysqlapi]#
kill测试 :
[iyunv@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
[iyunv@typhoeus79 mysqlapi]#
[iyunv@typhoeus79 mysqlapi]# kill 15460
[iyunv@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
[iyunv@typhoeus79 mysqlapi]# kill -9 17431
[iyunv@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
[iyunv@typhoeus79 mysqlapi]#
使用kill以及kill -9 进程的id都发生变化
使用supervisorctl进行程序重启 :
[iyunv@typhoeus79 mysqlapi]# supervisorctl stop all
mysqlapi: stopped
[iyunv@typhoeus79 mysqlapi]# ps aux |grep 8005 |grep -v grep
[iyunv@typhoeus79 mysqlapi]# supervisorctl start all
mysqlapi: started
[iyunv@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
[iyunv@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:
[iyunv@typhoeus79 20140515]# supervisorctl
http://10.75.xxx.xxx:9001 refused connection
原因在于没有开启inet_http_server,配置文件默认使用分号注释每行
将inet_http_server前面的分号去除后,重新开启supervisord
[iyunv@typhoeus79 20140515]# /etc/init.d/supervisord stop
Stopping supervisord: [ OK ]
[iyunv@typhoeus79 20140515]# supervisord -c /etc/supervisord.conf
[iyunv@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
访问界面:
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com