supervisor 安裝、配置、常用命令


 

前言

在 web 應用部署到線上后,需要保證應用一直處於運行狀態,在遇到程序異常、報錯等情況,導致 web 應用終止時,需要保證程序可以立刻重啟,繼續提供服務。

所以,就需要一個工具,時刻監控 web 應用的運行情況,管理該進程。

Supervisor 就是解決這種需求的工具,可以保證程序崩潰后,重新把程序啟動起來等功能。

簡介

Supervisor 是一個用 Python 寫的進程管理工具,可以很方便的用來在 UNIX-like 系統(不支持 Windows)下啟動、重啟(自動重啟程序)、關閉進程(不僅僅是 Python 進程)。

安裝

  1. Ubuntu系統下:apt-get install supervisor,通過這種方式安裝后,自動設置為開機啟動
  2. 也可以通過 pip install supervisor 進行安裝,但是需要手動啟動,然后設置為開機啟動(不推薦這種安裝方式)

Supervisor 配置

Supervisor 是一個 C/S 模型的程序,supervisord 是 server 端,supervisorctl 是 client 端。

supervisord

下面介紹 supervisord 配置方法。supervisord 的配置文件默認位於 /etc/supervisord.conf,內容如下(;后面為注釋):

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file) UNIX socket 文件,supervisorctl 會使用 chmod=0700 ; sockef file mode (default 0700) socket 文件的 mode,默認是 0700 [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) 日志文件,默認是 $CWD/supervisord.log pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) pid 文件 childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket 
#通過 UNIX socket 連接 supervisord,路徑與 unix_http_server 部分的 file 一致 ; 在增添需要管理的進程的配置文件時,推薦寫到 `/etc/supervisor/conf.d/` 目錄下,所以 `include` 項,就需要像如下配置。 ; 包含其他的配置文件 [include] files = /etc/supervisor/conf.d/*.conf ; 引入 `/etc/supervisor/conf.d/` 下的 `.conf` 文件

program 配置

program 的配置文件就寫在,

supervisord 配置中 include 項的路徑下:/etc/supervisor/conf.d/,然后 program 的配置文件命名規則推薦:app_name.conf

[program:app] ; 程序名稱,在 supervisorctl 中通過這個值來對程序進行一系列的操作 autorestart=True ; 程序異常退出后自動重啟 autostart=True ; 在 supervisord 啟動的時候也自動啟動 redirect_stderr=True ; 把 stderr 重定向到 stdout,默認 false environment=PATH="/home/app_env/bin" ; 可以通過 environment 來添加需要的環境變量,一種常見的用法是使用指定的 virtualenv 環境 command=python server.py ; 啟動命令,與手動在命令行啟動的命令是一樣的 user=ubuntu ; 用哪個用戶啟動 directory=/home/app/ ; 程序的啟動目錄 stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默認 50MB stdout_logfile_backups = 20 ; stdout 日志文件備份數 ; stdout 日志文件,需要注意當指定目錄不存在時無法正常啟動,所以需要手動創建目錄(supervisord 會自動創建日志文件) stdout_logfile = /data/logs/usercenter_stdout.log

需要注意:

  • 用 supervisord 管理時,gunicorn 的 daemon 選項需要設置為 False
  • 如果啟動命令需要包含workon,修改environment參數:environment=PATH="/home/username/.virtualenvs/myproject/bin"

supervisorctl 操作

supervisorctl 是 supervisord 的命令行客戶端工具,使用的配置和 supervisord 一樣,這里就不再說了。下面,主要介紹 supervisorctl 操作的常用命令:

輸入命令 supervisorctl 進入 supervisorctl 的 shell 交互界面(還是純命令行😓),就可以在下面輸入命令了。:

  • help # 查看幫助
  • status # 查看程序狀態
  • stop program_name # 關閉 指定的程序
  • start program_name # 啟動 指定的程序
  • restart program_name # 重啟 指定的程序
  • tail -f program_name # 查看 該程序的日志
  • update # 重啟配置文件修改過的程序(修改了配置,通過這個命令加載新的配置)

也可以直接通過 shell 命令操作:

  • supervisorctl status
  • supervisorctl update
  • ...

 

 

supervisor 監控nginx 一直在重啟的問題

supervisor 監控nginx 一直在重啟的問題
supervisor 監控nginx ,寫好配置文件之后,發現一直在重啟,排查之后發現是命令不對:

command = /usr/local/bin/nginx 這個命令默認是后台啟動,但是supervisor不能監控后台程序,所以supervisor就一直執行這個命令。

加上-g 'daemon off;'這個參數可解決這問題,這個參數的意思是在前台運行。

command = /usr/local/bin/nginx  -g 'daemon off;'

完整的supervisor 監控nginx 配置如下:


[program:nginx]
 
command = /usr/local/bin/nginx  -g 'daemon off;'
 
stdout_logfile=/Users/ddios/nginx_stdout.log
 
stdout_logfile_maxbytes=10MB
 
stderr_logfile=/Users/ddios/nginx_stderr.log
 
user = ddios
 
stderr_logfile_maxbytes=10MB
 
autostart=true
 
autorestart=true

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM