supervisor 监控nginx意外停止后自启

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项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值