一、简介
1、supervisor是什么
superviosr是一个Linux/Unix系统上进程监控和管理的工具,它由python编写,可以用pip安装。supervisor能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启
2、为什么要使用supervisor
supervisor可以实现单个或成组地控制进程,它可以把web服务的进程组当作自己的子进程,在子进程挂掉时重启。对一组进程进行统一管理是linux没有的功能
3、supervisor组件
supervisord是管理其他进程的主进程
supervisorctl是supervisor客户端的命令行工具
二、安装与配置
#安装
yum install python-setuptools -y
easy_install supervisor #生成一个配置文件
echo_supervisord_conf > /usr/local/supervisord.conf #修改[inet_http_server]配置,来启用WEB管理界面
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
username=root ; default is no username (open server)
password=123456 ; default is no password (open server) [include]
files = /usr/local/supervisor_conf/*.ini ;配置文件存放地址
三、使用
#进程测试脚本
[root@localhost~]#cat /usr/local/scripts/t.sh
#!/bin/bash
while true;do
date "+%Y-%m-%d %T" >>/tmp/t1.txt
sleep 1
done [root@localhost~]chmod +x /usr/local/scripts/t.sh #创建监控配置目录
mkdir /usr/local/supervisor_conf/ #创建配置文件
[root@localhost~]#cat /usr/local/supervisor_conf/test_t.ini
[program:test_t1]
command=/bin/bash /usr/local/scripts/t.sh ;启动命令
autostart=true ; 在supervisord启动的时候也自动启动
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
priority=2 ; 进程启动优先级,默认999,值小的优先启动
stdout_logfile=/tmp/test_t1.log ;日志文件 #启动Supervisor服务
[root@localhostscripts]#supervisord -c /usr/local/supervisord.conf #查看与测试
[root@localhostscripts]#tailf /tmp/t1.txt #有文本一直写入 [root@localhostscripts]#ps -ef|grep bash |grep t.sh
root 12588 12587 0 09:45 ? 00:00:00 /bin/bash /usr/local/scripts/t.sh #提前监控,测试杀掉进程
[root@localhost~]#ps -ef|grep bash |grep t.sh
root 12588 12587 0 09:45 ? 00:00:00 /bin/bash /usr/local/scripts/t.sh
[root@localhost~]kill 12588
[root@localhost~]#ps -ef|grep bash |grep t.sh
root 13112 12587 0 09:49 ? 00:00:00 /bin/bash /usr/local/scripts/t.sh
4、交互终端
supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程。
[root@localhostscripts]#supervisorctl
Server requires authentication
Username:root
Password: test_t1 RUNNING pid 13262, uptime 0:02:13
supervisor> help default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version supervisor> stop all
supervisor> start all
五、web管理
#修改监听端口
vim /usr/local/supervisord.conf
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; ip_address:port specifier, *:port for all iface
#重启
[root@localhost~]#ps -ef|grep supervisord
root 13261 1 0 09:50 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /usr/local/supervisord.conf
root 14051 12337 0 09:56 pts/7 00:00:00 grep --color=auto supervisord
[root@localhost~]#kill 13261
[root@localhost~]supervisord -c /usr/local/supervisord.conf
浏览器访问
六、Supervisor配置systemctl服务
vim /usr/lib/systemd/system/supervisor.service
[Unit]
Description=supervisor
After=network.target [Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /usr/local/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s [Install]
WantedBy=multi-user.target ##干掉原先的supervisor进程
[root@localhost~]#ps -ef|grep super
root 14065 1 0 09:57 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /usr/local/supervisord.conf
[root@localhost~]#kill 14065
#使用systemctl启动
[root@localhost~]#systemctl start supervisor.service
[root@localhost~]#systemctl status supervisor.service
● supervisor.service - supervisor
Loaded: loaded (/usr/lib/systemd/system/supervisor.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2019-04-28 10:03:34 CST; 1s ago
Process: 14931 ExecStart=/usr/bin/supervisord -c /usr/local/supervisord.conf (code=exited, status=0/SUCCESS)
Main PID: 14934 (supervisord)
CGroup: /system.slice/supervisor.service
├─14934 /usr/bin/python /usr/bin/supervisord -c /usr/local/supervisord.conf
├─14935 /bin/bash /usr/local/scripts/t.sh
└─14937 sleep 1 4月 28 10:03:34 localhost.localdomain systemd[1]: Starting supervisor...
4月 28 10:03:34 localhost.localdomain systemd[1]: Started supervisor.
#开机自启
[root@localhost~]systemctl enable supervisor.service
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisor.service to /usr/lib/systemd/system/supervisor.service.