使用 supervisor 管理进程

摘要:
Supervisor可以在Linux和Mac OS X上运行。Supervisor功能强大,提供了很多功能,但我们可能只需要使用其中的一小部分。为了方便起见,我们将配置分为两部分:管理程序和应用程序。首先,让我们看看supervisord的配置文件。

Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动。

Supervisor 可以运行在 Linux、Mac OS X 上。如前所述,supervisor 是 Python 编写的,所以安装起来也很方便,可以直接用 pip :

sudo pip install supervisor
或
apt-get install supervisor

如果是 Ubuntu 系统,还可以使用 apt-get 安装。

Supervisor 相当强大,提供了很丰富的功能,不过我们可能只需要用到其中一小部分。安装完成之后,可以编写配置文件,来满足自己的需求。为了方便,我们把配置分成两部分:supervisord(supervisor 是一个 C/S 模型的程序,这是 server 端,对应的有 client 端:supervisorctl)和应用程序(即我们要管理的程序)。

首先来看 supervisord 的配置文件。安装完 supervisor 之后,可以运行echo_supervisord_conf 命令输出默认的配置项,也可以重定向到一个配置文件里:

echo_supervisord_conf > /etc/supervisord.conf

去除里面大部分注释和“不相关”的部分,我们可以先看这些配置:

文件路径如下: /etc/supervisor

; 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

每个项目配置文件路径如下

通常放到项目中通过软连接的方式到以下目录 ln -s cplgame.supervisor.conf  /etc/supervisor/conf.d

/etc/supervisor/conf.d

改变文件权限 不然会出现软连接失效的情况

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


supervisorctl status:查看所有进程的状态
supervisorctl stop es:停止es
supervisorctl start es:启动es
supervisorctl restart es: 重启es
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序

踩过的坑
1、unix:///var/run/supervisor/supervisor.sock no such file

     问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错

     解决办法:supervisord -c /etc/supervisord.conf 

2、command中指定的进程已经起来,但supervisor还不断重启

     问题描述:command中启动方式为后台启动,导致识别不到pid,然后不断重启,本人使用的是elasticsearch,command                        指定的是$path/bin/elasticsearch -d,踩到的坑

     解决办法:supervisor无法检测后台启动进程的pid,而supervisor本身就是后台启动守护进程,因此不用担心这个

3、启动了多个supervisord服务,导致无法正常关闭服务

    问题描述:在运行supervisord -c /etc/supervisord.conf 之前,我直接运行过supervisord -c /etc/supervisord.d/xx.conf                         ,导致有些进程被多个superviord管理,无法正常关闭进程。

    解决办法: 使用 ps -fe | grep supervisord 查看所有启动过的supervisord服务,kill相关的进程。

免责声明:文章转载自《使用 supervisor 管理进程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Vue浏览器调试工具VueTools安装以及使用微信小程序-获取input值的两种方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

centos 7 部署 yapi

yapi是高效、易用、功能强大的API管理平台,旨在为开发、产品、测试人员提供更优雅的接口管理服务 部署环境要求: 1)nodejs(7.6+) 2)mongodb(2.6+) 一、安装nodejs  1、首先安装wget  yum install -y wget 如果已经安装了可以跳过该步, -y 参数代表跳过询问 2、下载nodejs最新的bin包 下...

supervisor 使用实践

注意:Supervisor要求Python 2.4或更高版本,但不支持Python 3的任何版本。 1. supervisor 介绍 Supervisor是一个客户端/服务器系统,允许用户在类UNIX操作系统上控制大量进程。 Supervisor组件:   Supervisord: 服务器部分为Supervisord,负责启动子程序,相应客户端的命令,记录...

Supervisor的作用与配置

原文链接 supervisor supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通...

logstash使用supervisord

  关于supervisord: supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起。 一...

supervisord 安装、配置

1.配置好yum源后,可以直接安装 yum install supervisor 2.输入以下指令查看安装已完成 supervisord --version 3.生成配置文件 echo_supervisord_conf > /etc/supervisord.conf 4.修改supervisor配置文件 vim /etc/supervisord.c...

Centos7 k8s v1.5.2二进制部署安装-kube-proxy

一、安装kube-proxy 1、概述 运行在每个节点上,监听 API Server 中服务对象的变化,再通过管理 IPtables 来实现网络的转发 Kube-Proxy 目前支持三种模式: UserSpace:k8s v1.2 后就已经淘汰 IPtables:目前默认方式 IPVS--推荐,支持7层:需要安装ipvsadm、ipset 工具包和加载 i...