使用 supervisor 管理進程

Supervisor (http://supervisord.org) 是一個用 Python 寫的進程管理工具,能夠很方便的用來啓動、重啓、關閉進程(不單單是 Python 進程)。除了對單個進程的控制,還能夠同時啓動、關閉多個進程,好比很不幸的服務器出問題致使全部應用程序都被殺死,此時能夠用 supervisor 同時啓動全部應用程序而不是一個一個地敲命令啓動。web

 

Supervisor 能夠運行在 Linux、Mac OS X 上。如前所述,supervisor 是 Python 編寫的,因此安裝起來也很方便,能夠直接用 pip :服務器

sudo pip install supervisor
或
apt-get install supervisor

 

若是是 Ubuntu 系統,還可使用 apt-get 安裝。socket

 

 

Supervisor 至關強大,提供了很豐富的功能,不過咱們可能只須要用到其中一小部分。安裝完成以後,能夠編寫配置文件,來知足本身的需求。爲了方便,咱們把配置分紅兩部分:supervisord(supervisor 是一個 C/S 模型的程序,這是 server 端,對應的有 client 端:supervisorctl)和應用程序(即咱們要管理的程序)。ide

首先來看 supervisord 的配置文件。安裝完 supervisor 以後,能夠運行echo_supervisord_conf 命令輸出默認的配置項,也能夠重定向到一個配置文件裏:工具

 

echo_supervisord_conf > /etc/supervisord.conf

 

 

去除裏面大部分註釋和「不相關」的部分,咱們能夠先看這些配置:ui

文件路徑以下: /etc/supervisorthis

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.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

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.
#文件入口
[include]
files = /etc/supervisor/conf.d/*.conf

 

每一個項目配置文件路徑以下url

一般放到項目中經過軟鏈接的方式到如下目錄 ln -s cplgame.supervisor.conf  /etc/supervisor/conf.dspa

/etc/supervisor/conf.dpwa

改變文件權限 否則會出現軟鏈接失效的狀況

chmod 644 cplgame.supervisor.conf    lrwxrwxrwx

 

[program:cplgame]
command=/usr/local/bin/uwsgi -i uwsgi_config.ini              ; the program (relative uses PATH, can take args)
process_name=%(program_name)s      ; process_name expr (default %(program_name)s)
directory=/home/op/cplgame/run      ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
stopsignal=QUIT                ; signal used to kill process (default TERM)
stopwaitsecs=2                 ; max num secs to wait b4 SIGKILL (default 10)
stopasgroup=true               ; send stop signal to the UNIX process group (default false)
killasgroup=true               ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

 

經常使用命令

裝載
supervisorctl reload

查看狀態
supervisorctl status



重啓
sudo supervisorctl restart cplgame
相關文章
相關標籤/搜索