正确离线装置supervisor

supervisor简介

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

supervisor 会波及三个重要的命令

  • supervisord

能够了解成supervisor的服务端

运行supervisor时会启动一个过程supervisord,它负责启动所治理的过程,并将所治理的过程作为本人的子过程来启动,而且能够在所治理的过程呈现解体时主动重启

  • supervisorctl

能够了解成supervisor的客户端

supervisorctl是命令行管理工具,能够用如下命令来进行子过程的治理,如:

  • echo_supervisord_conf

用来生成默认的配置文件,个别生成默认文件为 supervisor.conf

supervisor装置的后期筹备(全副应用压缩包的形式)

supervisor是用python写的运行在linux下的过程管理工具,装置supervisor的时候依赖 python的一些包,它依赖:python、setuptools、meld3

1、python装置

能够参考此处装置python:https://www.cnblogs.com/winte...

下载python链接:https://www.python.org/ftp/py...

下载相应的压缩包,如:wget https://www.python.org/ftp/py...

tar -xvf Python-3.6.8.tgz

2、setuptools装置

https://pypi.python.org/pypi/...

下载压缩包,用tar解压压缩包,装置,如

tar -zxvf setuptools-0.6c11.tar.gz cd setuptools-0.6c11 python setup.py install

3、meld3装置

https://pypi.python.org/pypi/...

wget https://pypi.python.org/packa...

tar -zxvf meld3-1.0.2.tar.gz cd meld3-1.0.2 python setup.py install

4、supervisor装置

tar -zxvf supervisor-3.3.1.tar.gz cd supervisor-3.3.1 python setup.py install

此处 supervisor装置后会生成咱们关怀的如下几个可执行程序:

  • supervisord 门路
supervisord:  /usr/local/bin/supervisord
  • supervisorctl 门路
supervisorctl:  /usr/local/bin/supervisorctl
  • echo_supervisord_conf 门路
echo_supervisord_conf:  /usr/local/bin/echo_supervisord_conf

验证supervisor是否装置胜利

supervisorctl --help

supervisor配置

创立必要目录和文件

  • 创立 /etc/supervisor 目录
mkdir /etc/supervisor
  • echo_supervisord_conf 生成supervisord.conf
echo_supervisord_conf  > /etc/supervisor/supervisord.conf

批改/etc/supervisor/supervisord.conf文件内容

  • 将unix_http_server 下的 file 门路改掉,如下:
[unix_http_server] file=/var/run/supervisor.sock  ; (the path to the socket file)

  • 将supervisord 下的logfile 门路 和 pidfile 门路 改掉,如下:
[supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

  • 将include 下的 files 门路改掉,如下:
[include] files = conf.d/*.conf

根据上述批改的门路,创立相应的文件和增加权限

创立文件:

touch /var/run/supervisor.sock touch /var/log/supervisord.log touch /var/run//supervisord.pid mkdir /etc/supervisor/conf.d 增加权限 chmod 777 /var/run chmod 777 /var/log

编写本人须要监控的子过程程序

子程序配置文件的编写

/etc/supervisor/conf.d 目录下创立本人的子过程配置文件,如:

/etc/supervisor/conf.d 创立文件 test.conf

[program:test] process_name=%(program_name)s_%(process_num)02d command= go run main.go         # 运行命令 directory=/home/qb/              # 运行文件目录 autostart=true                   # 主动启动 autorestart=true                 # 主动重新启动 user=root                       # 哪一个用户执行 redirect_stderr=true             #重定向谬误

子程序的编写

其中上述 main.go 是我长期写的一个demo程序,简略循环1秒钟写入字符串到文件中(写到文件中是为了便于查看成果),如:

package mainimport (   "fmt"   "os/exec"   "time")func main() {     for {               cmd := exec.Command("/bin/bash", "-c", `echo 222 >> a.txt`)               stdout, err := cmd.StdoutPipe()              if err != nil {                    fmt.Printf("Error:can not obtain stdout pipe for command:%s\n", err)                 return        }       //执行命令       if err := cmd.Start(); err != nil {         fmt.Println("Error:The command is err,", err)                return         }       fmt.Println(stdout)   time.Sleep(time.Second * time.Duration(1))   }

启动superviosr

1、unlink一下

unlink /var/run/supervisor.sock

2、启动supervisor:

supervisord -c /etc/supervisor/supervisord.conf

3、查看程序运行状态

supervisorctlstatus

查看成果

子程序是 每距离1秒钟 向 a.txt文件中写入字符串 222

进一步测试

  • 删掉子过程,测试supervisor是否会拉起子过程

将supervisor退出到开启启动服务中

1、写服务文件

vim /usr/lib/systemd/system/supervisord.service

supervisord.service

[Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/local/bin/supervisorctl shutdown ExecReload=/usr/local/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target

2、使能服务

systemctl enable supervisord

3、验证是否使能胜利

systemctl is-enabled supervisord

呈现enable阐明胜利

4、重启机器验证 supervisor是否随开机

常用命令提醒

service supervisord start #启动程序 service supervisord stop #进行程序 service supervisord status #查看状态 supervisorctl shutdown #敞开所有工作 supervisorctl stop|start 子程序名字 #启动或进行服务 supervisorctl status #查看所有工作状态

异常情况提供参考

切记肯定要把目录名,文件名 全副写正确,不要本人坑了本人!!

1、error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.6/socket.py line: 567

解决形式: 应用指定配置文件的形式解决

supervisord -c /etc/supervisor/supervisord.conf

2、初始化配置文件 echo_supervisord_conf 报错

解决形式:手动创立配置文件门路

mkdir /etc/supervisorecho_supervisord_conf > /etc/supervisor/supervisord.conf

3、操作supervisorctl 查看没有test过程

解决形式:/etc/supervisor/supervisord.conf 中 关上 include 模块

4、Unlinking stale socket /tmp/supervisor.sock

解决形式:

unlink /var/run/supervisor.sock

作者:小魔童哪吒