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
相关文章
相关标签/搜索