項(xiàng)目中經(jīng)常會(huì)已守護(hù)進(jìn)程執(zhí)行一些腳本文件或者是監(jiān)控某些服務(wù)的健康狀態(tài)需要crontab定時(shí)去檢查比較麻煩,搜索發(fā)現(xiàn)可以用Supervisor來管理這些進(jìn)程,這里做一個(gè)使用記錄。
安裝:這里只展示一種方法,應(yīng)該還有其他方法,本人沒有測(cè)試不做展示了。
pip install supervisor //安裝
echo_supervisord_conf > /etc/supervisord.conf //生成配置文件路徑可以自定義。
配置文件里參數(shù)很多這里也不做詳細(xì)記錄了,不設(shè)置也不會(huì)影響程序運(yùn)行,在配置文件結(jié)尾我們需要放開一段注釋來引入我們自定義的配置文件,我把每一個(gè)需要監(jiān)控的進(jìn)程創(chuàng)建了一個(gè)配置文件放在了/etc/supervisord/。
[include]
files = /etc/supervisord/*.conf
接下貼一下/etc/supervisord下的詳細(xì)配置文件,這些參數(shù)基本夠用了,詳細(xì)的參數(shù)在主配置文件里都有,可以去查看。
[program:beanstalk] ; :后邊是給要監(jiān)聽某個(gè)服務(wù)起了個(gè)名字
command=/usr/local/bin/beanstalkd -b /data/server/binlog/ -l 0.0.0.0 ;這是我們啟動(dòng)的命令,
stdout_logfile=/opt/run.log ;日志文件
autostart=true ;如果設(shè)置為true,當(dāng)supervisord啟動(dòng)的時(shí)候,進(jìn)程會(huì)自動(dòng)啟動(dòng)
autorestart=true ;設(shè)置為隨 supervisord 重啟而重啟,值可以是false、true
startsecs=60 ;程序啟動(dòng)后等待多長(zhǎng)時(shí)間后才認(rèn)為程序啟動(dòng)成功,默認(rèn)是10秒
stopasgroup=true
killasgroup=true
startretries=1
redirect_stderr=true
接下來就是啟動(dòng)supervisor了。
supervisord -c /etc/supervisord.conf
管理我們的進(jìn)程就需要另一個(gè)命令了。
supervisorctl status:查看所有進(jìn)程的狀態(tài)
supervisorctl stop beanstalk:停止beanstalk
supervisorctl start es:?jiǎn)?dòng)beanstalk
supervisorctl restart beanstalk: 重啟beanstalk
supervisorctl update :配置文件修改后可以使用該命令加載新的配置
supervisorctl reload: 重新啟動(dòng)配置中的所有程序
貼張圖我管理了兩個(gè)進(jìn)程。

如果需要新增配置文件或者有改動(dòng)直接運(yùn)行
supervisorctl update
更新就可以了。
對(duì)了還有一個(gè)比較人性的功能就是可以web頁(yè)面管理,在配置文件里開啟。
[inet_http_server] ; inet (TCP) server disabled by default
port=*:9502 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)

可以停止,重啟進(jìn)程很方便。
在supervisor安裝啟動(dòng)過程中還是遇到了一些問題,比如啟動(dòng)報(bào)錯(cuò),如果在啟動(dòng)或重新加載配置文件有報(bào)錯(cuò)大家自行解決下吧,因?yàn)槲覉?jiān)信自己解決的問題才會(huì)記憶深刻,提高自己的水平。