聲明: 博客引用來源:https://blog.csdn.net/qq_37997978/article/details/83311177 建議看原版,更爲詳細python
介紹: Supervisor( http://supervisord.org/ )是用Python開發的一個client/server服務,是Linux/Unix系統下的一個進程管理工具,不支持Windows系統。它能夠很方便的監聽、啓動、中止、重啓一個或多個進程。用Supervisor管理的進程,當一個進程意外被殺死,supervisort監聽到進程死後,會自動將它從新拉起,很方便的作到進程自動恢復的功能,再也不須要本身寫shell腳原本控制。由於Supervisor是Python開發的,安裝前先檢查一下系統否安裝了Python2.4以上版本。web
一、安裝python自動化工具shell
yum install python-setuptoolsbootstrap
二、yum安裝Supervisor (阿里雲驗證經過)
yum install supervisorvim
三、easy_install 安裝Supervisor(若是yum安裝沒法找到資源,能夠經過easy_install)
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
easy_install supervisor (直接運行此命令驗證經過)安全
四、 在etc下建立目錄,並賦權限
mkdir -m 700 -p /etc/supervisor服務器
五、 在目錄「 /etc/supervisor」下建立配置文件 「supervisord.conf」
echo_supervisord_conf > /etc/supervisor/supervisord.conf工具
六、 修改配置文件
vim /etc/supervisor/supervisord.conf阿里雲
在文件末尾添加,注意首尾需無空格,需頂格(這裏要刪除行頭的分號 「;」)
內容以下:spa
[include] files=/etc/supervisor/conf.d/*.conf
七、Web管理界面: 出於安全考慮,默認配置是沒有開啓web管理界面,須要修改supervisord.conf配置文件打開http訪權限,將下面的配置修改,可外網訪問:
內容以下:
[inet_http_server] ; inet (TCP) server disabled by default port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface) username=user ; (default is no username (open server)) password=user ; (default is no password (open server))
若是是最後纔想起配置外網訪問,請使用如下命令使配置生效:
supervisord -c /etc/supervisor/supervisord.conf
效果以下: 輸入外網地址 http://***:9001
八、 在目錄「/etc/supervisor」下建立dotnet core 進程配置文件存放目錄「conf.d」
mkdir -m 700 /etc/supervisor/conf.d
九、 建立進程配置文件 test.conf," test "能夠爲dotnet core 入口dll文件名字 ,插入內容,注意首尾需無空格,需頂格
vim /etc/supervisor/conf.d/test.conf
[program:test] command=dotnet test1.dll directory=/home/test1/ stderr_logfile=/var/log/test1.error.log stdout_logfile=/var/log/test1.stdout.log environment=ASPNETCORE_ENVIRONMENT=Production user=root stopsignal=INT autostart=true autorestart=true startsecs=3
內容說明:
[program:test] ;顯示名稱
command= dotnet test1.dll ;運行命令,啓動dotnet進程
directory=/home/test1/ ;test1目錄
stderr_logfile=/var/log/test1.error.log ;錯誤日誌文件
stdout_logfile=/var/log/test1.stdout.log ;日誌文件
environment=ASPNETCORE_ENVIRONMENT=Production ;進程環境變量
user=root ;進程執行用戶
stopsignal=INT ;
autostart=true ;若是設置爲true,當supervisord啓動的時候,進程會自動重啓。
autorestart=true ;程序崩潰時自動重啓,重啓次數是有限制的,默認爲3次
startsecs=3 ;啓動 3 秒後沒有異常退出,就看成已經正常啓動了
十、啓動supervisor(手動啓動)
supervisord -c /etc/supervisor/supervisord.conf
查看已啓動服務:pstree -p | grep supervisord
注意:
(1)每次修改配置文件後需進入supervisorctl,執行reload, 改動部分才能生效
(2)兩個命令
supervisord : supervisor的服務器端部分,用於supervisor啓動
supervisorctl:啓動supervisor的命令行窗口,在該命令行中可執行start、stop、status、reload等操做。
查看狀態: supervisorctl status
執行supervisorctl reload的命令能夠重啓監控服務: supervisorctl reload
啓動某個進程: supervisorctl start test1
重啓某個進程: supervisorctl restart test1
中止某一個進程: supervisorctl stop test1
中止所有進程: supervisorctl stop all
載入最新的配置文件,並按新的配置啓動、管理全部進程: supervisorctl reload
十一、 建立supervisor 自啓動服務(開機啓動Supervisor服務)
vim /etc/systemd/system/supervisor.service
內容以下:
[Unit] Description=supervisor [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 daemon-reload
設置服務開機啓動,即設置enable
systemctl enable supervisor.service
啓動服務
systemctl start supervisor.service