2020年年後工做中需開發一支持多數據源自動上報業務數據的程序,程序開發完部署上線時須要對其進程進行自動管理,否則哪天程序down了還不知,可就麻煩了,因此這裏選用了強大的supervisor,如下文章爲學習及實操時的筆記,不正確處請指出
python
Supervisor官網:http://supervisord.org
Supervisor是用Python開發的一套通用的進程管理程序,能將一個普通的命令行進程變爲後臺daemon,並監控進程狀態,異常退出時能自動重啓。它是經過fork/exec的方式把這些被管理的進程看成supervisor的子進程來啓動,這樣只要在supervisor的配置文件中,把要管理的進程的可執行文件的路徑寫進去便可。也實現當子進程掛掉的時候,父進程能夠準確獲取子進程掛掉的信息的,能夠選擇是否本身啓動和報警。
supervisor還提供了一個功能,能夠爲supervisord或者每一個子進程,設置一個非root的user,這個user就能夠管理它對應的進程。git
[maaiqiang@localhost ~]$ pip install supervisor
github
[maaiqiang@localhost ~]$ ls supervisor-4.2.0.tar.gz [maaiqiang@localhost ~]$ tar xf supervisor-4.2.0.tar.gz [maaiqiang@localhost ~]$ cd supervisor-4.2.0 [maaiqiang@localhost ~]$ python setup.py install
[unix_http_server] file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 會使用 ;chmod=0700 ;socket文件的mode,默認是0700 ;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid ;[inet_http_server] ;HTTP服務器,提供web管理界面 ;port=127.0.0.1:9001 ;Web管理後臺運行的IP和端口,若是開放到公網,須要注意安全性 ;username=user ;登陸管理後臺的用戶名 ;password=123 ;登陸管理後臺的密碼 [supervisord] logfile=/tmp/supervisord.log ;日誌文件,默認是 $CWD/supervisord.log logfile_maxbytes=50MB ;日誌文件大小,超出會rotate,默認 50MB,若是設成0,表示不限制大小 logfile_backups=10 ;日誌文件保留備份數量默認10,設爲0表示不備份 loglevel=info ;日誌級別,默認info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ;pid 文件 nodaemon=false ;是否在前臺啓動,默認是false,即以 daemon 的方式啓動 minfds=1024 ;能夠打開的文件描述符的最小值,默認 1024 minprocs=200 ;能夠打開的進程數的最小值,默認 200 [supervisorctl] serverurl=unix:///tmp/supervisor.sock ;經過UNIX socket鏈接supervisord,路徑與unix_http_server部分的file一致 ;serverurl=http://127.0.0.1:9001 ; 經過HTTP的方式鏈接supervisord ; [program:xx]是被管理的進程配置參數,xx是進程的名稱 [program:xx] command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序啓動命令 autostart=true ; 在supervisord啓動的時候也自動啓動 startsecs=10 ; 啓動10秒後沒有異常退出,就表示進程正常啓動了,默認爲1秒 autorestart=true ; 程序退出後自動重啓,可選值:[unexpected,true,false],默認爲unexpected,表示進程意外殺死後才重啓 startretries=3 ; 啓動失敗自動重試次數,默認是3 user=tomcat ; 用哪一個用戶啓動進程,默認是root priority=999 ; 進程啓動優先級,默認999,值小的優先啓動 redirect_stderr=true ; 把stderr重定向到stdout,默認false stdout_logfile_maxbytes=20MB ; stdout 日誌文件大小,默認50MB stdout_logfile_backups = 20 ; stdout 日誌文件備份數,默認是10 ; stdout 日誌文件,須要注意當指定目錄不存在時沒法正常啓動,因此須要手動建立目錄(supervisord 會自動建立日誌文件) stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out stopasgroup=false ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程 killasgroup=false ;默認爲false,向進程組發送kill信號,包括子進程 ;包含其它配置文件 [include] files = relative/directory/*.ini ;能夠指定一個或多個以.ini結束的配置文件
[maaiqiang@localhost ~]$ mkdir /etc/supervisor [maaiqiang@localhost ~]$ echo_supervisord_conf > /etc/supervisor/supervisored.conf
;conf.d 爲配置表目錄,須要手動建立,方便各子進程配置管理 [include] files = conf.d/*.conf
[maaiqiang@localhost ~]$ mkdir /etc/supervisor/conf.d
[maaiqiang@localhost ~]$ vi UmsScheduler.conf
web
[program:UmsScheduler] command=/usr/bin/python /home/mq/ums/py/UmsMonitor/UmsScheduler.py directory=/home/mq/ums/py/UmsMonitor/ autostart=true autorestart=true stderr_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-err.log stdout_logfile=/home/mq/ums/py/UmsMonitor/log/UmsScheduler-out.log user=mq
[maaiqiang@localhost ~]$ supervisord -c /etc/supervisor/supervisord.conf
apache
# dservice for systemd (CentOS 7.0+) # by ET-CS (https://github.com/ET-CS) [Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
[maaiqiang@localhost ~]$ systemctl enable supervisord.service # 加入開啓自啓動項 [maaiqiang@localhost ~]$ systemctl is-enabled supervisord #檢查是否爲開啓啓動項 [maaiqiang@localhost ~]$ systemctl start supervisord.service # 啓動並加載默認配置文件
[maaiqiang@localhost ~]$ ps -ef | grep 'supervisord'
[maaiqiang@localhost ~]$ ps -ef | grep 'UmsScheduler'
這裏,能夠模擬發生異常將其進程殺死,查看其進程是否可以自動啓動tomcat
supervisorctl status # 查看全部進程的狀態 supervisorctl stop UmsScheduler # 中止UmsScheduler supervisorctl start UmsScheduler # 啓動UmsScheduler supervisorctl restart UmsScheduler # 重啓UmsScheduler supervisorctl update # 配置文件修改後使用命令加載新的配置 supervisorctl reload # 從新啓動配置中的全部程序