1.安装Python包管理工具(easy_install):
yum install python-setuptools -y
2.安装Supervisor:
easy_install supervisor
3.配置Supervisor应用守护
通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
然后查看路径下的supervisord.conf。在文件尾部添加如下配置。
[include]
files = conf.d/*.conf
conf.d 为配置表目录的文件夹,需要手动创建
mkdir /etc/supervisor/conf.d
为你的程序创建一个.conf文件,放在目录"/etc/supervisor/conf.d/"下
vim supervisor_nginx.conf
[program:supervisor_nginx]
command=nginx -c /etc/nginx/nginx.conf -g ‘daemon off;’
directory=/usr/local/nginx/sbin
autorestart=true
stderr_logfile=/var/log/supervisor_nginx.err.log
stdout_logfile=/var/log/supervisor_nginx.out.log
user=root
说明:
第一行:supervisor_nginx为进程名字,自定义
第二行:进程启动命令, -g 'daemon off;'代表nginx在前台运行
第三行:命令所在目录
第四行:程序意外退出是否自动重启
第五行:错误日志
第六行:输出日志
第七行:进程执行的用户身份
command = /usr/local/bin/nginx 这个命令默认是后台启动,但是supervisor不能监控后台程序,所以supervisor就一直执行这个命令。
加上-g 'daemon off;'这个参数可解决这问题,这个参数的意思是在前台运行。
4.启动supervisor:
supervisord -c /etc/supervisor/supervisord.conf
5.配置Supervisor开机启动
vim /usr/lib/systemd/system/supervisord.service
dservice for systemd (CentOS 7.0+)
by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
执行自启命令:
systemctl enable supervisord
验证是否已设置为自启动:
systemctl is-enabled supervisord
6.测试:
查看nginx的进程号,然后执行nginx -s stop,之后再查看nginx进程号,发现进程号已经改变了,说明nginx在被停止后被supervisor自动拉起来了。
7.常用命令:
supervisorctl restart ;重启指定应用
supervisorctl stop ;停止指定应用
supervisorctl start ;启动指定应用
supervisorctl restart all ;重启所有应用
supervisorctl stop all ;停止所有应用
supervisorctl start all ;启动所有应用
例如:
supervisorctl restart supervisor_nginx就是重启nginx项目