使用 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
相关文章
相关标签/搜索