3分鐘學會如何上手supervisor看門狗

軟硬件環境

  • centos7.6.1810 64bit前端

    cat /etc/redhat-release #查看系統版本
  • supervisor 3.4.0python

  • python 2.7.5程序員

supervisor簡介

supervisor是一個用python語言編寫的進程管理工具,它能夠很方便的監聽、啓動、中止、重啓一個或多個進程。當一個進程意外被殺死,supervisor監聽到進程死後,能夠很方便的讓進程自動恢復,再也不須要程序員或系統管理員本身編寫代碼來控制。web

supervisord安裝

yum install -y epel-release
yum install -y supervisor

啓動&開啓自啓

systemctl start supervisord
systemctl enable supervisord

其餘命令

systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord

supervisor的web端

supervisor提供了基於web的控制,管理員能夠經過在頁面上點點按鈕便可完成對進程的啓動、重啓等操做,甚是方便。shell

進入配置文件,開啓對web端的支持vim

vim /etc/supervisord.conf

若是提供給外部訪問,須要將port改成本機ip地址centos

#取消10-13行註釋,前面數字是行號
[inet_http_server]         ; inet (TCP) server disabled by default
port=192.168.26.121:9001   ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

配置完成後重啓服務工具

systemctl restart supervisord

supervisord應用配置

進入supervisord配置文件測試

cat /etc/supervisord.conf

經過配置文件最後一行看到centos7

[include]
files = supervisord.d/*.ini

也就是說,咱們全部的應用配置文件都保存在這個目錄下,以.ini格式命名保存的,能夠自行修改地址,但不要修改後綴

那咱們來建立一個受監控的應用吧

建立測試python配置

建立一個名稱叫作python的應用程序配置

vim /etc/supervisord.d/python.ini

配置文件內容,其中command就是咱們應用程序啓動須要執行的命令

[program:python] #這裏的python就是咱們顯示在web前端以及終端的監控名稱
command=python /tmp/supervisordtest/test.py  #咱們要監控的文件地址
autostart=true
autorestart=true
startsecs=1
startretries=3
redirect_stderr=true
stdout_logfile=/tmp/supervisordtest/access_python.log   #日誌地址,可自行配置目錄
stderr_logfile=/tmp/supervisordtest/error_python.log    #日誌地址,可自行配置目錄

建立test.py

mkdir /tmp/supervisordtest
vim /tmp/supervisordtest/test.py

程序內容:開啓一個死循環,不停的打印內容

while True:
   print(100)

重啓supervisord使配置文件生效

systemctl restart supervisord

查看應用是否正常啓動

一、命令查看

systemctl status supervisord

二、可視化web查看

web端能夠重啓,中止,清理日誌,查看日誌等多個操做

image-20200607153351958

supervisor相關的幾個命令

安裝完畢,會生成3個系統命令supervisorctlsupervisordecho_supervisord_conf

  1. supervisord,運行supervisor時會啓動一個進程supervisord,它負責啓動所管理的進程,並將所管理的進程做爲本身的子進程來啓動,並且能夠在所管理的進程出現崩潰時自動重啓

  2. supervisorctl是命令行管理工具,能夠用來執行 start stop restart 等命令,來對這些子進程進行管理, 如

    sudo supervisorctl start demoweb

    其中demoweb是進程的名稱, 詳細的命令及說明見下面的這張表

    命令 說明
    supervisorctl start program_name 啓動某個進程
    supervisorctl stop program_name 中止某個進程
    supervisorctl restart program_name 重啓某個進程
    supervisorctl status program_name 查看某個進程的狀態
    supervisorctl stop all 中止所有進程 | \
    supervisorctl reload 載入最新的配置文件,重啓全部進程
    supervisorctl update 根據最新的配置,重啓配置更改過的進程,未更新的進程不受影響
  3. echo_supervisord_conf

    用來生成默認的配置文件(默認配置文件,內容很是齊全且都有註釋,適合用時查閱,用法是這樣的

    echo_supervisord_conf > test.conf
相關文章
相關標籤/搜索