一、安装supervisor
备注:supervisor 只能管理前台进程的服务,比如 npm run  这些 ,一些后台运行的服务无法管理【后台运行的服务可以用systemd 进行管理】
 
1、安装epel源
yum install epel-release
yum install -y supervisor
2、创建supervisor工作目录
mkdir /etc/supervisor
mkdir /etc/supervisor/conf.d
 
ps: 删除默认的工作目录:
rm -rf /etc/supervisord.d
rm -rf /etc/supervisord.conf
 
3、生成配置文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
 
4、修改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
username=admin       		; default is no username (open server)
password=XXXXXXXX           ; default is no password (open server)


...............
[include]
files = conf.d/*.conf
 

 

5、创建一个需要运行的实例
 
配置详解:

 

...................
[program:theprogramname]    						 #这里的theprogramname就是我们显示在web前端以及终端的监控名称
command=/bin/cat             						 #运行启动命令
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1                    ; number of processes copies to start (def 1)
directory=/tmp               						 #定义工作目录,command 执行目录
umask=022                     ; umask for process (default None)
priority=999                  						 #进程启动优先级,默认999,值小的优先启动
autostart=true               						 #在supervisord启动的时候也自动启动
startsecs=1                   						 #启动1秒后没有异常退出,就表示进程正常启动了,默认为1秒
startretries=3               						 #启动失败自动重试次数,默认是3
autorestart=false       						     #程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启;建议设置为false,当手动kill 下线服务,supervisor不会自动拉起

exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
stopsignal=QUIT               ; signal used to kill process (default TERM)
stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
stopasgroup=false     								#默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false     								#默认为false,向进程组发送kill信号,包括子进程
user=root                   						#用哪个用户启动进程,默认是root

redirect_stderr=false          #把stderr重定向到stdout,默认false

stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=20MB   						#stdout 日志文件大小,默认50MB
stdout_logfile_backups=10     						#sdout 日志文件备份数,默认是10
stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false   ; emit events on stdout writes (default false)

stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
stderr_events_enabled=false   ; emit events on stderr writes (default false)


environment=A="1",B="2"       ; process environment additions (def no adds)
serverurl=AUTO                ; override serverurl computation (childutils)
...................
 
vim /etc/supervisor/conf.d/test.conf
[program:test]
command=python /tmp/test.py
directory=/tmp
autostart=true
startsecs=1
startretries=3
redirect_stderr=true
autorestart=false
 
6、配置supervisor自启动

 

#修改默认的启动配置文件
vim /usr/lib/systemd/system/supervisord.service

 

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

[Install]
WantedBy=multi-user.target
#重新加载
[root@localhost etc]# systemctl daemon-reload
 
#设置开始自启动
[root@localhost etc]# systemctl enable supervisord
 

 

#启动supervisord
[root@localhost etc]# systemctl start supervisord
 
 

 

7、登录supervisor 控制台
输入用户名/密码
 
可以看到运行的 实例
 
 

 

 

 

二、安装CeSi
 CeSi(Centralized Supervisor Interface) 是 Supervisor 官方推荐的集中化管理 Supervisor 实例的 Web UI,该工具是用 Python 编写,基于 Flask Web 框架 。Supervisor 进程,功能比较简单,通过 CeSi 可以集中管理各个服务器节点的进程,在 Web 界面就可以轻松管理各个服务的启动、关闭、重启等,很方便使用。
 
项目地址:https://github.com/gamegos/cesi
本文环境: centos7 
 
1、安装依赖

 

$ sudo yum install -y git epel-release
$ sudo yum install -y python34 python34-pip python34-venv
 
2、安装 Cesi
# 设置环境变量并创建安装目录
$ export CESI_SETUP_PATH=~/cesi
$ mkdir ${CESI_SETUP_PATH}
$ cd ${CESI_SETUP_PATH}

#下载cesi软件包,并解压;这里是2.7.1版本
$ wget https://github.com/gamegos/cesi/releases/download/v2.7.1/cesi-extended.tar.gz -O cesi.tar.gz
$ tar -xvf cesi.tar.gz

# 创建venv虚拟环境
$ python3 -m venv venv

# 激活venv虚拟环境
$ source venv/bin/activate

#在venv虚拟环境中,使用pip3管道安装依赖
(venv) $ pip3 install -r requirements.txt

#后面有个坑,无法登录【AttributeError: can't set attribute】 需要安装这个 upgrade flask-sqlalchemy lib.
(venv) $ pip3  install -U Flask-SQLAlchemy


# 失效venv虚拟环境
(venv) [root@localhost cesi]# deactivate
[root@localhost cesi]# 

 

 
3、配置 Cesi 
修改我们上面定义的  ${CESI_SETUP_PATH}/defaults/cesi.conf.toml 配置文件
${CESI_SETUP_PATH} 相当于 /root/cesi/
cesi启动会调用此配置文件

 

[root@localhost cesi]# vim /root/cesi/defaults/cesi.conf.toml
............
[cesi]
# Database Uri
database = "sqlite:///users.db"                         # Relative path
# Etc
#database = "sqlite:////opt/cesi/< version >/users.db"  # Absolute path
#database = "postgres://<user>:<password>@localhost:5432/<database_name>"
#database = "mysql+pymysql://<user>:<password>@localhost:3306/<database_name>"
activity_log = "activity.log"   # File path for CeSI logs
admin_username = "admin"        # Username of admin user
admin_password = "admin"        # Password of admin user
    
    

# This is the definition section for new supervisord node.
# [[nodes]]
# name = "api"          # (String) Unique name for supervisord node.
# environment = ""      # (String) The environment name provides logical grouping of supervisord nodes. It can be used as filtering option in the UI.
# username = ""         # (String) Username of the XML-RPC interface of supervisord Set nothing if no username is configured
# password = ""         # (String) Password of the XML-RPC interface of supervisord. Set nothing if no username is configured
# host = "127.0.0.1"    # (String) Host of the XML-RPC interface of supervisord
# port = "9001"         # (String) Port of the XML-RPC interface of supervisord

# Default supervisord nodes

#添加一个node的supervisord
[[nodes]]
name = "172.16.20.252"
environment = "test"
username = "admin"
password = "XXXXXXXX"
host = "172.16.10.252"
port = "9001"

 

 
4、启动 Cesi 

 

#以虚拟环境直接运行项目
[root@localhost cesi]# /root/cesi/venv/bin/python3 /root/cesi/cesi/run.py  --config-file  /root/cesi/defaults/cesi.conf.toml

 

出现http://X.X.X.X:5000 表示运行成功

 

 
5、登录 Cesi 控制台
在浏览器中输入 http://172.16.10.252:5000 打开管理界面如下:
默认用户名/密码:   admin/admin
 
 

 

 

 

 

 

 

6、使用supervisord 管理CeSi 自启动
vim  /etc/supervisor/conf.d/cesi.conf
[program:cesi]
command=/root/cesi/venv/bin/python3 /root/cesi/cesi/run.py  --config-file  /root/cesi/defaults/cesi.conf.toml
autostart=true
startsecs=1
starttries=3
redirect_stderr=false
autorestart=false
 
重新加载新的配置
supervisorctl update

 

 
7、supervisorctl  命令行工具使用
更新新的配置到supervisord
supervisorctl update
 
重新启动配置中的所有程序
supervisorctl reload
 
启动某个进程(program_name=你配置中写的程序名称)
 supervisorctl start program_name
 
停止某一进程 (program_name=你配置中写的程序名称)
supervisorctl stop program_name
 
重启某一进程 (program_name=你配置中写的程序名称)
supervisorctl restart program_name
 
停止全部进程
supervisorctl stop all