天天看點

Linux Supervisor的安裝與使用入門Linux Supervisor的安裝與使用入門安裝配置啟動常用指令常見問題

Linux Supervisor的安裝與使用入門

在linux或者unix作業系統中,守護程序(Daemon)是一種運作在背景的特殊程序,它獨立于控制終端并且周期性的執行某種任務或等待處理某些發生的事件。由于在linux中,每個系統與使用者進行交流的界面稱為終端,每一個從此終端開始運作的程序都會依附于這個終端,這個終端被稱為這些程序的控制終端,當控制終端被關閉的時候,相應的程序都會自動關閉。但是守護程序卻能突破這種限制,它脫離于終端并且在背景運作,并且它脫離終端的目的是為了避免程序在運作的過程中的資訊在任何終端中顯示并且程序也不會被任何終端所産生的終端資訊所打斷。它從被執行的時候開始運轉,直到整個系統關閉才退出。

此處的建立守護程序,是指釋出在Linux上 asp.net core 程式的dotnet xxx.dll指令的宿主程序建立一個守護程序。

在 Linux 上有很多可以管理程序的工具,我們使用 Supervisor 來做這個事情。原因有三點:

1、它是微軟官方文檔推薦的,降低學習成本。

2、它并不一定是最好的,但一定是文檔最全的。

3、nohup 無法背景挂起的,它也許可以幫助你

​ Supervisor是采用 Python(2.4+) 開發的,它是一個允許使用者管理 基于 Unix 系統程序的 Client/Server 系統,提供了大量功能來實作對程序的管理。

官方文檔:

http://supervisord.org/

安裝

yum install -y epel-release
yum install -y supervisor           

預設配置檔案:/etc/supervisord.conf

程序管理配置檔案放到:/etc/supervisord.d/目錄下即可

配置

配置檔案中 /etc/supervisord.conf 注意include的檔案夾以及字尾

[include]
files = supervisord.d/*.ini             

下面是配置Tomcat程序的一個例子:

[program:tomcat]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true           

我的配置檔案

[program:pierced]
command=/root/pierced/ding -config=/root/pierced/ding.cfg -subdomain=community 8081
stdout_logfile=/root/pierced/logs/catalina.out
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true           

啟動

啟動并檢視是否生效,執行以下指令

supervisord -c /etc/supervisord.conf
ps -ef | grep pierced

# 傳回内容
[root@localhost supervisord.d]# ps -ef | grep pierc
root     20964 20918  0 16:54 ?        00:00:00 /root/pierced/ding -config=/root/pierced/ding.cfg -subdomain=community 8081
root     21721 27871  0 16:55 pts/3    00:00:00 grep --color=auto pierc           

常用指令

sudo service supervisor stop 停止supervisor服務
 
sudo service supervisor start 啟動supervisor服務
 
supervisorctl shutdown #關閉所有任務
 
supervisorctl stop|start program_name #啟動或停止服務
 
supervisorctl status #檢視所有任務狀态

# 如果新增了配置檔案 可以在指令行使用 update更新子任務
sudo supervisorctl update    # 預設更新全部 後面加 任務檔案名 啟動特定任務           

管理supervisor子任務

可以使用

supervisorctl

指令在控制台檢視目前運作的任務

Linux Supervisor的安裝與使用入門Linux Supervisor的安裝與使用入門安裝配置啟動常用指令常見問題
#  控制台操作
supervisor> restart app   # 重新開機任務  app任務名, 任務檔案.ini中設定的  [program:app]
supervisor> stop app   # 停止任務
supervisor> update app # 啟動app
supervisor> status app  # 檢視狀态
supervisor> exit  # 退出
supervisor> help  # 一共這麼多指令

default commands (type help <topic>):
=====================================
add    clear  fg        open  quit    remove  restart   start   stop  update 
avail  exit   maintail  pid   reload  reread  shutdown  status  tail  version           

5、 配置supervisord開機啟動

a.在指定目錄下建立檔案supervisord.service

vim /usr/lib/systemd/system/supervisord.service           

b.輸入以下内容:

[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           

提示:

Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.           

驗證是否為開機啟動:

systemctl is-enabled supervisord           

enabled

表示設定成功!

至此,建立supervisor守護程序完畢。

常見問題

1.unix:///var/run/supervisor.sock no such file

問題描述:安裝好supervisor沒有開啟服務直接使用supervisorctl報的錯

解決辦法:supervisord -c /etc/supervisord.conf

2.command中指定的程序已經起來,但supervisor還不斷重新開機

問題描述:command中啟動方式為背景啟動,導緻識别不到pid,然後不斷重新開機,這裡使用的是elasticsearch,command指定的是$path/bin/elasticsearch -d

解決辦法:supervisor無法檢測背景啟動程序的pid,而supervisor本身就是背景啟動守護程序,是以不用擔心這個

3.使用過程中發現了一個很大的問題

就是執行supervisorctl shutdown 或者 supervisorctl stop的時候 任務雖然被關閉了,但是我們執行的指令還在運作 并沒有被kill

服務啟動後,是多程序模式,就需要注意一下,項目代碼内是否有處理主程序被 kill,然後向子程序發送 kill signal,如果沒有就需要到相應服務的 supervisor管理配置檔案聲明:stop 主程序時(主程序接收到 stop 信号),然後向同組的子程序發送 kill signal,保證子程序徹底退出,加入如下配置項即可:

killasgroup=true
stopasgroup=true