如今有一個進程須要每時每刻不斷的跑,可是這個進程又有可能因爲各類緣由有可能中斷。當進程中斷的時候我但願能自動從新啓動它,此時,就須要使用到了Supervisor。Supervisor起到守護進程的做用。html
https://pypi.python.org/pypi/supervisorpython
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。mysql
參照上面方法再進行安裝meld,安裝完成以後再次進行install便可。sql
測試安裝是否成功:echo_supervisord_conf api
配置文件:echo_supervisord_conf > /etc/supervisord.conf服務器
supervisord : supervisor的服務器端部分,啓動supervisor就是運行這個命令python2.7
supervisorctl:啓動supervisor的命令行窗口socket
需求是對本地的API進程進行守護,要求這個服務能在乎外中止後自動重啓。測試
API的執行命令爲:python2.6 /data1/guosong/mysqlapi/main/api_main.py --port=8005ui
修改配置文件:
[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]#
守護多個進程,修改/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
command=python26 /data1/guosong/mysqlapi/main/api_main.py --port=140%(process_num)02d
問題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
訪問界面: