supervisor 安裝、配置、經常使用命令

前言

在 web 應用部署到線上後,須要保證應用一直處於運行狀態,在遇到程序異常、報錯等狀況,致使 web 應用終止時,須要保證程序能夠馬上重啓,繼續提供服務。python

因此,就須要一個工具,時刻監控 web 應用的運行狀況,管理該進程。git

Supervisor 就是解決這種需求的工具,能夠保證程序崩潰後,從新把程序啓動起來等功能。github

簡介

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

安裝

  1. Ubuntu系統下:apt-get install supervisor,經過這種方式安裝後,自動設置爲開機啓動
  2. 也能夠經過 pip install supervisor 進行安裝,可是須要手動啓動,而後設置爲開機啓動(不推薦這種安裝方式)

Supervisor 配置

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

supervisord

下面介紹 supervisord 配置方法。supervisord 的配置文件默認位於 /etc/supervisord.conf,內容以下(;後面爲註釋):ubuntu

; 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.confapp

[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

須要注意:socket

  • 用 supervisord 管理時,gunicorn 的 daemon 選項須要設置爲 False
  • 若是啓動命令須要包含workon,修改environment參數:environment=PATH="/home/username/.virtualenvs/myproject/bin"

supervisorctl 操做

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

輸入命令 supervisorctl 進入 supervisorctl 的 shell 交互界面(仍是純命令行😓),就能夠在下面輸入命令了。:post

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

也能夠直接經過 shell 命令操做:

  • supervisorctl status
  • supervisorctl update
  • ...

參考

相關文章
相關標籤/搜索