1、介紹supervisor
Supervisor是用Python開發的一個client/server服務,是Linux/Unix系統下的一個進程管理工具,不支持Windows系統。它可以很方便的監聽、啟動、停止、重啟一個或多個進程。用Supervisor管理的進程,當一個進程意外被殺死,supervisort監聽到進程死后,會自動將它重新拉起,很方便的做到進程自動恢復的功能,不再需要自己寫shell腳本來控制。
2、安裝環境
# 安裝 python環境
yum install python-setuptools
# 安裝 Supervisor
easy_install supervisor
3、配置Supervisor
# 新建配置目錄命令:
mkdir /etc/supervisor
#生成supervisor的初始化配置文件 :
echo_supervisord_conf > /etc/supervisor/supervisord.conf
編輯配置supervisord.conf
vim supervisord.conf
主要是打開網絡訪問、默認啟動配置目錄
# 創建配置目錄
mkdir /etc/supervisor/conf.d
# 進入目錄
cd conf.d
#創建一個WebApplication1.conf配置
vi WebApplication1.conf
# 創建程序啟動配置
# 進程名稱
[program:WebApplication1]
# 執行命令
command=dotnet WebApplication1.dll
# 執行目錄
directory=/root/aspnetcoreapi
# 掉線是否自動重啟
autorestart=true
# 日志信息
stderr_logfile=/var/log/WebApplication1.err.log
stdout_logfile=/var/log/WebApplication1.out.log
# 環境.Net core
environment=ASPNETCORE_ENVIRONMENT=Production
# 執行用戶
user=root
stopsignal=INT
4、啟動Supervisor
# 啟動Supervisor
supervisord -c /etc/supervisor/supervisord.conf
#查看狀態
supervisorctl status
5、bash終端控制
#啟動Supervisor
supervisord -c /etc/supervisor/supervisord.conf
# 查看狀態
supervisorctl status
# 停止某個服務
supervisorctl stop WebApplication1
# 開始某個服務
supervisorctl start WebApplication1
# 重啟某個服務
supervisorctl restart WebApplication1
# 重啟Supervisor
supervisorctl reload
# 修改Supervisor
supervisorctl update
6、將supervisor配置為開機自啟動服務
# 編輯服務文件
vim /usr/lib/systemd/system/supervisord.service
# 內容
[Unit]
Description=Supervisor
[Service]
Type=forking
PIDFile=/var/run/supervisord.pid
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
# 保存退出 啟動服務
systemctl enable supervisord
# 查看啟動狀態 返回enabled成功
systemctl is-enabled supervisord
#成功之后,就可以使用如下命令管理supervisor服務了
# 停止
systemctl stop supervisord
# 啟動
systemctl start supervisord
# 狀態
systemctl status supervisord
# 重載
systemctl reload supervisord
# 重啟
systemctl restart supervisord
我們最后也可以看到我們將進程殺死之后馬上會啟動一個新的進程啟動程序