前言

守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。
supervisor 由 python 开发,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
supervisor 4.0.0以上版本适用于Python 3版本3.4或更高版本以及Python 2版本2.7。 适用于centos7.4
supervisor 3.0版本以上可以在Python 2.4或更高版本下工作,但不能在任何版本的Python 3下工作。 适用于centos6.7
注意:需要考虑本地python版本、setuptools版本与supervisor的兼容性

安装setuptools

按照官方步骤,先安装setuptools工具
官网下载压缩包,并上传到服务器指定路径下用tar解压压缩包,安装,如

1
2
3
tar -zxvf setuptools-0.6c11.tar.gz -C /usr/local/
cd /usr/local/setuptools-0.6c11
python setup.py install

安装meld3

安装supervisor过程中 最后执行 python setup.py install可能会出现以下情况

1
2
3
4
5
6
7
Searching for meld3>=1.0.0
Reading https://pypi.python.org/simple/meld3/
Couldn't find index page for 'meld3' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for meld3>=1.0.0
error: Could not find suitable distribution for Requirement.parse('meld3>=1.0.0')

说明当前python环境中缺少meld3模块,且supervisor对该模块要求是meld3>=1.0.0
需要到官网下载meld3_1.0.2版本

1
2
3
4
wget https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz -C /usr/local/  #md5=3ccc78cd79cffd63a751ad7684c02c91   
tar zxvf meld3-1.0.2.tar.gz
cd /usr/local/meld3-1.0.2
python setup.py install

安装supervisord

官网下载压缩包,并上传到服务器指定路径下
centos7.4 下载4.0.4 centos6.7下载3.3.1

1
2
3
tar -zxvf supervisor-3.3.1.tar.gz -C /usr/local/
cd /usr/local/supervisor-3.3.1
python setup.py install

通过如下命令可查看当前supervisor的版本信息

1
/usr/bin/supervisord -v

supervisor 配置

1
2
3
4
5
mkdir /etc/supervisor # 创建supervisor,若存在请忽略
mkdir /etc/supervisor/conf.d # 创建conf.d,若存在请忽略
mkdir /var/log/supervisor/ # 创建supervisor 的log文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf # 添加设置
vim /etc/supervisor/supervisord.conf # 修改配置文件

最后两行修改为:

1
2
[include]
files = /etc/supervisor/conf.d/*.ini

添加浩易备启动服务

1
vim /etc/supervisor/conf.d/hybcdpstart.ini

添加以下内容:

1
2
3
4
5
6
7
8
[program:hybcdp]
directory = /opt/cdpadmin/
command = /opt/cdpadmin/manage runserver 127.0.0.1:8000 --noreload
autostart = true
startsecs = 5
user = root
redirect_stderr = true
stdout_logfile = /var/log/supervisor/supervisord.log

启动supervisor

1
2
3
4
5
6
7
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl status
# 其余命令
supervisorctl update # 修改conf配置文件后重新加载
supervisorctl reload # 重启
supervisorctl start hybcdp # 启动浩易备进程
supervisorctl stop all # 停止进程

设置Supervisor服务开机自启动

1、Centos6.7版本:

1
2
3
4
5
6
7
8
9
vim /etc/init.d/supervisor  # 增加service配置
chmod +x /etc/init.d/supervisor # 设置脚本为可执行
chkconfig supervisor on # 设置服务开机启动
chkconfig --list supervisor
# 启停命令
service supervisor start
service supervisor stop
service supervisor restart
service supervisor status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
#
# supervisord This scripts turns supervisord on
#
# Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
#
# chkconfig: - 95 04
#
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# processname: supervisord
# config: /etc/supervisor/supervisord.conf
# pidfile: /var/run/supervisord.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
echo -n $"Starting supervisord: "
daemon "/usr/bin/supervisord -c /etc/supervisor/supervisord.conf"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}

stop() {
echo -n $"Stopping supervisord: "
killproc supervisord
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}

restart() {
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac

exit $RETVAL

2、Centos7.4版本:

1
2
3
4
5
6
7
8
vim /usr/lib/systemd/system/supervisord.service
chmod +x /usr/lib/systemd/system/supervisord.service
systemctl enable supervisord
systemctl is-enabled supervisord # 验证一下是否为开机启动
# 启停命令
systemctl start supervisord
systemctl stop supervisord
systemctl status supervisord
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#supervisord.service

[Unit]
Description=Supervisor daemon

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

参考链接

1、离线方式安装supervisor
2、CentOS7 下 离线安装 supervisor
3、Linux下supervisor安装与使用
4、Supervisor服务开机自启动
5、supervisor 常用命令的使用 及开机启动 开启守护
6、正确离线安装 supervisor
7、supervisor进程管理&开机自启