Supervisord守護Python程序
使用Python socket + HTML5 websocket寫一個web聊天室,上線使用時經常無故程序就被kill掉,剛開始的時候每次聊天聊天就會斷開伺服器,然後手動重啟程序,大家重新進入聊天室繼續聊。時間長了以後,發現這樣不是沒有根本解決辦法,因為每天聊天室的程序都會無故死掉。
Supervisord
supervisord的出現,結束了我這苦惱的問題,它可以幫你守護任何程序,當然如果它的程序也掛了就全都over了。實際情況是上線三個多月執行非常好,沒有發現程序掉過。
CentOS下安裝Supervisord
# yum install python-setuptools
# easy_install supervisor
建立配置檔案
# echo_supervisord_conf > /etc/supervisord.conf
修改配置檔案
# vi /etc/supervisord.conf
在末尾新增
?1 2 3 4 5 6 |
[program:chat]
command =python
/data0/htdocs/chat/main .py
priority=1
numprocs=1
autostart= true
autorestart= true
|
配置說明:
command 要執行的命令
priority 優先順序
numprocs 啟動幾個程序
autostart supervisor啟動的時候是否隨著同時啟動
autorestart 當程式over的時候,這個program會自動重啟,一定要選上
啟動Supervisord
# supervisord
檢視幫助
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[[email protected] core] # supervisord --help
supervisord -- run a
set of applications as daemons.
Usage: /usr/bin/supervisord
[options]
Options:
-c /--configuration
FILENAME -- configuration file
-n /--nodaemon
-- run in
the foreground (same as 'nodaemon true'
in config file )
-h /--help
-- print this usage message and exit
- v /--version
-- print supervisord version number and exit
-u /--user
USER -- run supervisord as this user (or numeric uid)
-m /--umask
UMASK -- use this umask
for daemon subprocess (default is 022)
-d /--directory
DIRECTORY -- directory to chdir to when daemonized
-l /--logfile
FILENAME -- use FILENAME as logfile path
-y /--logfile_maxbytes
BYTES -- use BYTES to limit the max size of logfile
-z /--logfile_backups
NUM -- number of backups to keep when max bytes reached
-e /--loglevel
LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j /--pidfile
FILENAME -- write a pid file
for the daemon process to FILENAME
-i /--identifier
STR -- identifier used for
this instance of supervisord
-q /--childlogdir
DIRECTORY -- the log directory for
child process logs
-k /--nocleanup
-- prevent the process from performing cleanup (removal of
old automatic child log files) at startup.
-a /--minfds
NUM -- the minimum number of file
descriptors for
start success
-t /--strip_ansi
-- strip ansi escape codes from process output
--minprocs NUM -- the minimum number of processes available
for start success
--profile_options OPTIONS -- run supervisord under profiler and output
results based on OPTIONS,
which is a comma-sep'd
list of
'cumulative' ,
'calls' , and /or
'callers' ,
e.g.
'cumulative,callers' )
|
啟動時指定配置檔案
# supervisord -c /etc/supervisord.conf
進入ctl模式
# supervisorctl
ctl中的簡單命令
help 檢視命令幫助
status 檢視狀態
stop XXX 停止某一個程序
start XXX 啟動某個程序
restart XXX 重啟某個程序
reload 載入最新的配置檔案,停止原有程序並按新的配置啟動、管理所有程序
update 根據最新的配置檔案,啟動新配置或有改動的程序,配置沒有改動的程序不會受影響而重啟。
測試
這裡以守護nginx程序來演示,首先在/etc/supervisord.conf加入
?1 2 3 4 5 6 |
[program:nginx]
command = /usr/local/nginx/sbin/nginx
priority=1
numprocs=1
autostart= true
autorestart= true
|
然後啟動supervisord
?1 2 3 |
[[email protected] core] # supervisord -c /etc/supervisord.conf
[[email protected] core] # ps -le | grep supervisord
1 S 0 14035 1 0 80 0 - 48722 poll_s ? 00:00:00 supervisord
|
檢視nginx的程序
?1 2 3 4 5 |
[[email protected] core] # ps -le | grep nginx
1 S 0 14037 1 0 80 0 - 56260 rt_sig ? 00:00:00 nginx
5 S 99 14038 14037 0 80 0 - 56363 ep_pol ? 00:00:00 nginx
5 S 99 14039 14037 0 80 0 - 56300 ep_pol ? 00:00:00 nginx
5 S 99 14040 14037 0 80 0 - 56300 ep_pol ? 00:00:00 nginx
|
殺掉nginx程序
?1 |
[[email protected] core] # kill -9 14037
|
然後接著重新檢視nginx程序
?1 2 3 4 |
[[email protected] core] # ps -le | grep nginx
5 S 99 14038 1 0 80 0 - 56363 ep_pol ? 00:00:00 nginx
5 S 99 14039 1 0 80 0 - 56300 ep_pol ? 00:00:00 nginx
4 S 0 14456 14035 0 80 0 - 56259 hrtime ? 00:00:00 nginx
|
起死回生了,並且pid已經由14037變成14038。搞定!
通過web管理
supervisord可以通過web管理程序以及檢視程序狀態,需要在配置檔案裡開啟
找到[inet_http_server]這一段,修改成
?1 2 3 4 |
[inet_http_server] ; inet (TCP) server disabled by default
port=*:9001 ; (ip_address:port specifier, *:port
for all iface)
username=admin ; (default is no username ( open
server))
password=123 ; (default is no password ( open
server))
|
其中port這個欄位要各位注意,如果*:9001表示允許所有ip訪問,如果指定單個IP可以 xx.xx.xx.xx:9001 這樣既可。如果你開啟了iptabls記得要在規則裡允許port指定的埠號。
然後儲存配置,重啟supervisord
# supervisord reload
啟用瀏覽器訪問效果:
注:supervisord跟python沒有任何關係,supervisord只是一個程序管理守護程式,它可以守護除Python以外的任何程式。