进程管理保护的工具—Supervisor

尾调薄雾
• 阅读 2805

一、supervisor简介&功能

简介:
supervisor 是python开发的一套进程管理的程序 ,能将一个普通的命令行进程变为后台daemon(保护进程),并监控进程状态,异常退出时能自动重启。

基本功能:

  • 1、它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。
  • 2、实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。
  • 3、supervisor还提供了一个功能,可以为supervisor或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。

注:本文以centos8为例。

二、supervisor安装

方法一、配置好yum源后,可以直接安装

yum install supervisor

方法二、Debian/Ubuntu可通过apt安装

apt-get install supervisor

方法三、pip安装

pip install supervisor

方法四、easy_install安装

easy_install supervisor

三、配置文件说明

3.1、supervisor.conf配置文件说明

(位置:/etc/supervisord.conf)


; Sample supervisor config file.

[unix_http_server]
file=/run/supervisor/supervisor.sock   ; (the path to the socket file) # socket对应的文件
;chmod=0700                 ; socket file mode (default 0700) # socket文件的mod,默认是0700
;chown=nobody:nogroup       ; socket file uid:gid owner # socket文件的owner,格式:uid:gid
;username=user              ; (default is no username (open server)) # 默认无用户名
;password=123               ; (default is no password (open server)) # 默认无密码

;[inet_http_server]         ; inet (TCP) server disabled by default # 默认关闭的tcp服务器,提供管理页面
;port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface) # Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
;username=user              ; (default is no username (open server)) # 登录管理后台的用户名
;password=123               ; (default is no password (open server)) # 登录管理后台的密码

[supervisord]
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log) # 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; 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:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; 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)
;autorestart=true              ; retstart at unexpected quit (default: true)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;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)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;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        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; 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 = supervisord.d/*.ini

3.2 、子配置文件说明

(子配置文件是我们最需要关注的,以及经常配置的文件)

子进程配置文件,可以添加在主配置文件的conf中,也可以编写一个配置文件,放在/etc/supervisor.d/目录下,以.ini作为扩展名(每个进程的配置文件都可以单独分拆也可以把相关的脚本放一起)。

如任意定义一个和脚本相关的项目名称的选项组(/etc/supervisord.d/test.ini):

例子:

#项目名
[program:test]
#脚本目录
directory=/opt/bin
#脚本执行命令
command=/usr/bin/python /opt/bin/test.py

#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=1

#脚本运行的用户身份 
user = www

#日志输出 
stderr_logfile=/tmp/test_stderr.log 
stdout_logfile=/tmp/test_stdout.log 
#把stderr重定向到stdout,默认 false
redirect_stderr = true
#stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 50M
#stdout日志文件备份数
stdout_logfile_backups = 20

四、supervisor常用命令

supervisord -c /etc/supervisord.conf #启动supervisor服务命令
supervisorctl status                #查看所有进程的状态
supervisorctl stop test             #停止设置的test进程
supervisorctl start test            #启动设置的test进程
supervisorctl restart test          #重新启动设置的test进程
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,然后不断重启,
解决办法:supervisor无法检测后台启动进程的pid,而supervisor本身就是后台启动守护进程,因此不用担心这个

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

问题描述:在运行supervisord -c /etc/supervisord.conf 之前,我直接运行过supervisord -c /etc/supervisord.d/xx.ini,导致有些进程被多个superviord管理,无法正常关闭进程。
解决办法:使用ps -ef | grep supervisord 查看所有启动过的supervisord服务,kill相关的进程。

更多精彩内容关注下面微信公众号~

进程管理保护的工具—Supervisor

点赞
收藏
评论区
推荐文章
GoCoding GoCoding
4年前
Supervisor 开始
Supervisor是Linux/Unix操作系统上的进程管理工具。本文介绍了于Ubuntu18上如何使用Supervisor开机启动、保活守护自己的服务进程。安装建议系统方式安装,可开机启动。bashsudoaptinstallsupervisorySystemd查看服务状态:bash$sudosystemctlstatu
Easter79 Easter79
3年前
think
Supervisor的安装与使用入门在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。由于在linux中,每个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端被称为这些进程的控制终端,当控制终端被关闭的时候,相
Easter79 Easter79
3年前
supervisor运行golang守护进程
Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由python编写,它提供了大量的功能来实现对进程的管理。程序的多进程启动,可以配置同时启动的进程数,而不需要一个个启动程序的退出码,可以根据程序的退出码来判断是否需要自动重启程序所产生日志的处理进程初始化的环境,包括目录,用户,umask,
Stella981 Stella981
3年前
Linux进程守护——Supervisor 使用记录
0、旁白Supervisor是个父进程,你要守护的进程会以Supervisor的子进程形式存在,所以老子才可以管儿子官网链接:http://supervisord.org/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsupervisord.org%2F)【5、参数
Stella981 Stella981
3年前
Linux进程管理工具 Supervisor详解
Supervisor安装与配置(linux/unix进程管理工具)Supervisor(http://supervisord.org(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsupervisord.org))是用Python开发的一个client/server服务,是Li
Wesley13 Wesley13
3年前
supervisor 管理进程
supervisor管理进程说明Supervisor是一个用Python写的进程管理工具,可以很方便的用来启动、重启、关闭进程.安装pipinstallsupervisor配置echo_supervisord_conf/etc/s
Easter79 Easter79
3年前
Supervisor中启动netcore网站
1.安装配置Supervisorsupervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程自动恢复的功
Stella981 Stella981
3年前
Linux fork() 系统调用
    在Linux中,当程序调用fork()函数时,系统会创建新的进程,为其分配资源(存储数据和代码的空间),然后把原来进程的所有值都复制到新进程中,只有少量数值与原来的进程值不同,相当于复制了本身。      fork()系统调用,被调用一次,却能返回两次:父进程和子进程各自返回一次。可以通过fork()的返回值的不同来区分父进程和子进程。
Easter79 Easter79
3年前
Supervisor离线安装、管理
Supervisor(http://supervisord.org/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsupervisord.org%2F))是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
Stella981 Stella981
3年前
Centos7.6上部署Supervisor来监控和操作各类服务
supervisor  是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不
Nginx工作原理
Nginx的进程模型Nginx服务器由一个Master进程和多个Worker进程组成:Master进程:管理Worker进程。对外接口:接收外部的操作(信号);对内转发:根据外部操作的不同,通过信号管理Worker;监听:监控Worker进程的运行状态,Worker进程异常终止后,自动重启Worker进程。Worker进程:所有Worker进程都是平等的,用于处理网络请求。进程数量:在nginx.conf中配置,一般设置为核心数,充分利用CPU资源,同时,避免进程数量过多,避免进程竞争CPU资源,增加上下文切换的损耗。