天天看點

supervisor的使用記錄

前言

為啥使用supervisor,因為最近給我的API服務增加了一個定時任務,需要背景一直運作一條指令行,當關掉這個指令行時任務就不會跑了,各種搜尋,定位到supervisor能解決這個問題。于是了解了一下,發現Supervisor還能解決Django項目不能自動啟動的問題。Supervisor是用Python開發的一套通用的程序管理程式,能将一個普通的指令行程序變為背景daemon,并監控程序狀态,異常退出時能自動重新開機。它是通過fork/exec的方式把這些被管理的程序當作supervisor的子程序來啟動,這樣隻要在supervisor的配置檔案中,把要管理的程序的可執行檔案的路徑寫進去即可。也實作當子程序挂掉的時候,父程序可以準确擷取子程序挂掉的資訊的,可以選擇是否自己啟動和報警。supervisor還提供了一個功能,可以為supervisord或者每個子程序,設定一個非root的user,這個user就可以管理它對應的程序。

supervisor安裝

centos7 系統yum安裝的supervisor是3.*.*版本,需要使用python的pip安裝最新版本(4.2.3)

pip3 install supervisor
           

由于本機python是編譯安裝,是以supervisor安裝後所在目錄為:

/usr/local/python3/bin/supervisorctl  (用戶端(用于和守護程序通信,發送管理程序的指令))
/usr/local/python3/bin/supervisord  (是supervisor的守護程序服務(用于接收程序管理指令))
/usr/local/python3/bin/echo_supervisord_conf (生成初始配置檔案程式)
           

然後要将這個路徑添加到環境變量中

将 PATH=$PATH:$HOME/bin:/usr/local/python3/bin
添加到使用者目錄檔案 .bash_profile 裡面, 其他能夠執行環境變量初始化的檔案也可以。
           

配置supervisor

mkdir -p /etc/supervisor/conf.d/
echo_supervisord_conf > /etc/supervisor/supervisord.conf
           

在/etc/supervisor/supervisord.conf 的[include]下添加:

;[include]
;files = relative/directory/*.ini
# 修改為:
[include]
files = /etc/supervisor/conf.d/*.conf
# (注意去掉分号,第一次安裝的時候就因為沒去掉分号出現了問題!);
           

配置開機自啟動

官方腳本

wget -O /usr/lib/systemd/system/supervisord.service  https://github.com/Supervisor/initscripts/raw/master/centos-systemd-etcs
           

手動編寫

# supervisord service for systemd (CentOS 7.0+)
[Unit]
Description=Supervisor daemon
After=network.target

[Service]
Type=forking
# 第一個參數必須是可執行檔案的絕對路徑,不接受替代
ExecStart=/usr/local/python3/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/local/python3/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/local/python3/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
           

設定開機啟動

systemctl enable supervisord.service
systemctl daemon-reload 
systemctl list-unit-files|grep enabled #檢視是否開啟成功

chmod 766 supervisord.service # 修改檔案權限
           

編輯監控程式檔案

在/etc/supervisor/conf.d 下建立程式檔案,比如cmd.conf

[program:somecmd]
command=/usr/local/python3/bin/python cmd.py
numprocs=1
autostart=true
autorestart=true
startretries=3
user=nfuser
redirect_stderr=true
stdout_logfile=/var/log/cmd-stdout.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=20
           

如果有多個程式,可以配置為組:

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=php-fpm,nginx,redis,mysql  ; 定義該組有哪些程式,程式之間用逗号分隔,注意,這些程式必須在前面用“[program:theprogramname]”子產品定義過。
;priority=999                        ; 啟動優先級,假設有多個組,每個組一個優先級,越小越優先執行 (預設 999)
           

啟動supervisor

手動啟動supervisor

supervisord -c /etc/supervisor/supervisord.conf
ps -ef | grep supervisord # 檢視服務是否已啟動
cat /var/log/cmd-stdout.log # 通過日志檢視
           

通過systemd指令啟動

systemctl status supervisord # 檢視supervisord狀态
systemctl start supervisord
systemctl restart supervisord
systemctl stop supervisord
           

修改cmd配置檔案後重新載入

supervisorctl reread
supervisorctl update
supervisorctl restart somecmd

           

修改/etc/supervisor/supervisord.conf後重新載入

supervisorctl reload
           

一些其他可能用到的指令

supervisorctl start programname      啟動某個程序  
supervisorctl stop programname       停止某個程序
supervisorctl restart programname    重新開機某個程序
supervisorctl start all     啟動全部程序
supervisorctl stop all      停止全部程序,注:start、restart、stop都不會載入最新的配置檔案。
supervisorctl status        檢視狀态
supervisorctl shutdown      關閉supervisor
           

示例

Django+uWsgi+supervisor

  • uWsgi配置
[uwsgi]
# Django-related settings
#uwsgi 備注
# start ===> uwsgi --ini /hainergy/script/uwsgi.ini
# reload ===> uwsgi --reload /hainergy/pidfile/uwsgi.pid
# stop ===> uwsgi --stop /hainergy/pidfile/uwsgi.pid

# the base directory (full path)
home = /root/.virtualenvs/py38
chdir = /home/web/dogdog
module = dogdog.wsgi:application
# master
master = True
pidfile=/hainergy/pidfile/uwsgi.pid
# daemonize = /hainergy/log/uwsgi.log
# logto = /hainergy/log/uwsgi.log
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe)
socket = 127.0.0.1:9999         # 雲伺服器内部ip
uid = root
gid = root
workers = 1
reload-mercy = 10
# clear environment on exit
vacuum = True
max-requests = 1000
# max virtual
limit-as = 1024   
reload-on-as =1024
buffer-size = 30000
#py-autoreload=1
           
  • nginx
location /static{
  alias /home/web/doggo/static;
}	

location /media {
  alias /home/web/doggo/media;
}

location / {
  include /usr/local/nginx/conf/uwsgi_params;
  uwsgi_pass 127.0.0.1:9999;
  # index  templates/talk.html talk.htm;
}
           
  • supervisor
[program:djangogo]
command=/usr/local/python3/bin/uwsgi --ini /hainergy/script/uwsgi.ini
user=root
autorestart=true
autostart=true
startretries=3
redirect_stderr=true
startsecs=5
stdout_logfile=/hainergy/log/aigisss.log
stopasgroup=true
killasgroup=true
priority=999
           

Django+django_q

  • supervisor
[program:qcluster]
directory=/home/web/doggo
command=/root/.virtualenvs/aigisss_py/bin/python manage.py qcluster
stopasgroup = true
user=root
stdout_logfile=/hainergy/log/qcluster.log
           

node+supercisor

  • supervisor
[program:tilemap]
directory=/home/server/mbserver
command=/usr/local/nodejs/bin/pm2 start app.js
user=root
autorestart=true
autostart=true
startretries=3
redirect_stderr=true
startsecs=5
stdout_logfile=/hainergy/log/tilemap.log
stopasgroup=true
killasgroup=true
priority=999
           

繼續閱讀