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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章