使用Supervisor守護Python進程


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

 訪問界面:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM