supervisor是一个用python语言编写的进程管理工具,它能够很方便的监听、启动、中止、重启一个或多个进程。当一个进程意外被杀死,supervisor监听到进程死后,能够很方便的让进程自动恢复,再也不须要程序员或系统管理员本身编写代码来控制。html
能够理解成supervisor的服务端python
运行supervisor时会启动一个进程supervisord,它负责启动所管理的进程,并将所管理的进程做为本身的子进程来启动,并且能够在所管理的进程出现崩溃时自动重启linux
能够理解成supervisor的客户端ios
supervisorctl是命令行管理工具,能够用以下命令来进行子进程的管理,如:程序员
用来生成默认的配置文件,通常生成默认文件为 supervisor.confvim
supervisor安装的前期准备(所有使用压缩包的方式)bash
supervisor是用python写的运行在linux下的进程管理工具,安装supervisor的时候依赖 python的一些包,它依赖:python、setuptools、meld3socket
能够参考此处安装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
https://pypi.python.org/pypi/...
下载压缩包,用tar解压压缩包,安装,如
tar -zxvf setuptools-0.6c11.tar.gz cd setuptools-0.6c11 python setup.py install
tar -zxvf meld3-1.0.2.tar.gz cd meld3-1.0.2 python setup.py install
tar -zxvf supervisor-3.3.1.tar.gz cd supervisor-3.3.1 python setup.py install
此处 supervisor安装后会生成咱们关心的以下几个可执行程序:
supervisord: /usr/local/bin/supervisord
supervisorctl: /usr/local/bin/supervisorctl
echo_supervisord_conf: /usr/local/bin/echo_supervisord_conf
验证supervisor是否安装成功
supervisorctl --help
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
修改/etc/supervisor/supervisord.conf文件内容
[unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file)
[supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
[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 main import ( "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)) }
unlink /var/run/supervisor.sock
supervisord -c /etc/supervisor/supervisord.conf
supervisorctlstatus
子程序是 每间隔1秒钟 向 a.txt文件中写入字符串 222
vim /usr/lib/systemd/system/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
systemctl enable supervisord
systemctl is-enabled supervisord
出现enable说明成功
经常使用命令提示
service supervisord start #启动程序 service supervisord stop #中止程序 service supervisord status #查看状态 supervisorctl shutdown #关闭全部任务 supervisorctl stop|start 子程序名字 #启动或中止服务 supervisorctl status #查看全部任务状态
切记必定要把目录名,文件名 所有写正确,不要本身坑了本身!!
解决方式: 使用指定配置文件的方式处理
supervisord -c /etc/supervisor/supervisord.conf
解决方式:手动建立配置文件路径
mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf
解决方式:/etc/supervisor/supervisord.conf 中 打开 include 模块
解决方式:
unlink /var/run/supervisor.sock
做者:小魔童哪吒