关于linux:3分钟学会如何上手supervisor看门狗

6次阅读

共计 2333 个字符,预计需要花费 6 分钟才能阅读完成。

软硬件环境

  • centos7.6.1810 64bit
  cat /etc/redhat-release #查看零碎版本
  • supervisor 3.4.0
  • python 2.7.5

supervisor 简介

supervisor是一个用 python 语言编写的 过程治理 工具,它能够很不便的监听、启动、进行、重启一个或多个过程。当一个过程意外被杀死,supervisor监听到过程死后,能够很不便的让过程主动复原,不再须要程序员或系统管理员本人编写代码来管制。

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 的管制,管理员能够通过在页面上点点按钮即可实现对过程的启动、重启等操作,甚是不便。

进入配置文件,开启对 web 端的反对

vim /etc/supervisord.conf

如果提供给内部拜访,须要将 port 改为本机 ip 地址

# 勾销 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

通过配置文件最初一行看到

[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

查看利用是否失常启动

1、命令查看

systemctl status supervisord

2、可视化 web 查看

web 端能够重启,进行,清理日志,查看日志等多个操作

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
正文完
 0