Supervisor的作用与配置

更新时间:2023-05-14 05:57:23 阅读: 评论:0

Supervisor的作⽤与配置
supervisor管理进程,是通过fork/exec的⽅式将这些被管理的进程当作supervisor的⼦进程来启动,所以我们只需要将要管理进程的可执⾏⽂件的路径添加到supervisor的配置⽂件中就好了。此时被管理进程被视为supervisor的⼦进程,若该⼦进程异常中断,则⽗进程可以准确的获取⼦进程异常中断的信息,通过在配置⽂件中设置autostart=ture,可以实现对异常中断的⼦进程的⾃动重启。
安装supervisor
$ sudo apt-get install supervisor
配置⽂件
安装完supervisor后,输⼊以下命令可得到配置⽂件:
$ echo_supervisord_conf
或者:
$ cat /etc/f
配置⽂件如下(分号;表⽰注释):
; supervisor config file
[unix_http_rver]
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 ction must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
;
added by defining them in parate rpcinterface: ctions
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
rverurl=unix:///var/run/supervisor.sock ; u a unix:// URL  for a unix socket
; The [include] ction can just contain the "files" tting.  This
; tting can list multiple files (parated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themlves.
[include]
files = /etc/supervisor/conf.d/*.conf
以上配置⽂件⽤到⼏个部分:
[unix_http_rver]:这部分设置HTTP服务器监听的UNIX domain socket
file: 指向UNIX domain socket,即file=/var/run/supervisor.sock
chmod:启动时改变supervisor.sock的权限
[supervisord]:与supervisord有关的全局配置需要在这部分设置
logfile: 指向记录supervisord进程的log⽂件
pidfile:pidfile保存⼦进程的路径
childlogdir:⼦进程log⽬录设为AUTO的log⽬录
[supervisorctl]:
rverurl:进⼊supervisord的URL,对于UNIX domain sockets, 应设为 unix:///absolute/path/to/file.so
ck [include]:如果配置⽂件包含该部分,则该部分必须包含⼀个files键:
files:包含⼀个或多个⽂件,这⾥包含了/etc/supervisor/conf.d/⽬录下所有的.conf⽂件,可以在该⽬录下增加我们⾃⼰的配置⽂件,在该配置⽂件中增加[program:x]部分,⽤来运⾏我们⾃⼰的程序,如下:
[program:x]:配置⽂件必须包括⾄少⼀个program,x是program名称,必须写上,不能为空
华丽的辞藻
command:包含⼀个命令,当这个program启动时执⾏
directory:执⾏⼦进程时supervisord暂时切换到该⽬录
ur:账户名
startcs:进程从STARING状态转换到RUNNING状态program所需要保持运⾏的时间(单位:秒)
redirect_stderr:如果是true,则进程的stderr输出被发送回其stdout⽂件描述符上的supervisord
stdout_logfile:将进程stdout输出到指定⽂件
stdout_logfile_maxbytes:stdout_logfile指定⽇志⽂件最⼤字节数,默认为50MB,可以加KB、MB或
GB等单位
stdout_logfile_backups:要保存的stdout_logfile备份的数量
⽰例如下,在⽬录/etc/supervisor/conf.d/下创建f,并加⼊:
;/etc/supervisor/conf.f
[program:awesome]
command    = /usr/bin/env python3 /srv/awesome/www/app.py
directory  = /srv/awesome/www
ur        = www-data
startcs  = 3
redirect_stderr        = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 10
stdout_logfile          = /srv/awesome/log/app.log
配置完后,先进⼊/srv/awesome/⽬录下创建log⽬录,之后启动supervisor:
$ sudo supervisord -f
supervisor基本命令(后四个命令可以省略“-f”):
supervisord -f                      通过配置⽂件启动supervisor
supervisorctl -f status              查看状态
supervisorctl -f reload              重新载⼊配置⽂件重新启动配置中的所有程序
supervisorctl -f update              更新新的配置到supervisord
supervisorctl -f start [all]|[x] 启动所有/指定的程序进程 supervisorctl -f stop [all]|[x] 关闭所有/指定的程序进程
执⾏服务(运⾏app.py):
$ sudo supervisorctl start awesome
如果supervisor遇到错误,可以在/var/log/supervisor/supervisord.log中查看⽇志;
如果app运⾏出现问题,可以在/srv/awesome/log/app.log中查看⽇志。
配置⽂件详解
[unix_http_rver]
file=/tmp/supervisor.sock  ; socket⽂件的路径,supervisorctl⽤XML_RPC和supervisord通信就是通过它进⾏
的。如果不设置的话,supervisorctl也就不能⽤了
不设置的话,默认为none。⾮必须设置
;chmod=0700                ; 这个简单,就是修改上⾯的那个socket⽂件的权限为0700
不设置的话,默认为0700。⾮必须设置
;chown=nobody:nogroup      ; 这个⼀样,修改上⾯的那个socket⽂件的属组为ur.group
不设置的话,默认为启动supervisord进程的⽤户及属组。⾮必须设置
;urname=ur              ; 使⽤supervisorctl连接的时候,认证的⽤户
不设置的话,默认为不需要⽤户。⾮必须设置
;password=123              ; 和上⾯的⽤户名对应的密码,可以直接使⽤明码,也可以使⽤SHA加密
如:{SHA}82ab876d1387bfafe46cc1c8a2ef074eae50cb1d
默认不设置。。。⾮必须设置
;[inet_http_rver]        ; 侦听在TCP上的socket,Web Server和远程的supervisorctl都要⽤到他
不设置的话,默认为不开启。⾮必须设置
;port=127.0.0.1:9001        ; 这个是侦听的IP和端⼝,侦听所有IP⽤ :9001或*:9001。
这个必须设置,只要上⾯的[inet_http_rver]开启了,就必须设置它
;urname=ur              ; 这个和上⾯的uinx_http_rver⼀个样。⾮必须设置
;
password=123              ; 这个也⼀个样。⾮必须设置
[supervisord]                ;这个主要是定义supervisord这个服务端进程的⼀些参数的
这个必须设置,不设置,supervisor就不⽤⼲活了
logfile=/tmp/supervisord.log ; 这个是supervisord这个主进程的⽇志路径,注意和⼦进程的⽇志不搭嘎。
默认路径$CWD/supervisord.log,$CWD是当前⽬录。。⾮必须设置
logfile_maxbytes=50MB        ; 这个是上⾯那个⽇志⽂件的最⼤的⼤⼩,当超过50M的时候,会⽣成⼀个新的⽇志⽂件。当设置为0时,表⽰不限制⽂件⼤⼩
默认值是50M,⾮必须设置。
logfile_backups=10          ; ⽇志⽂件保持的数量,上⾯的⽇志⽂件⼤于50M时,就会⽣成⼀个新⽂件。⽂件数量⼤于10时,最初的⽼⽂件被新⽂件覆盖,⽂件数量将保持为10
当设置为0时,表⽰不限制⽂件的数量。
大方之家默认情况下为10。。。⾮必须设置
loglevel=info                ; ⽇志级别,有critical, error, warn, info, debug, trace, or blather等
默认为info。。。⾮必须设置项
pidfile=/tmp/supervisord.pid ; supervisord的pid⽂件路径。
默认为$CWD/supervisord.pid。。。⾮必须设置
nodaemon=fal              ; 如果是true,supervisord进程将在前台运⾏
默认为fal,也就是后台以守护进程运⾏。。。⾮必须设置
minfds=1024                  ; 这个是最少系统空闲的⽂件描述符,低于这个值supervisor将不会启动。
系统的⽂件描述符在这⾥设置cat /proc/sys/fs/file-max
默认情况下为1024。。。⾮必须设置
minprocs=200                ; 最⼩可⽤的进程描述符,低于这个值supervisor也将不会正常启动。
ulimit  -u这个命令,可以查看linux下⾯⽤户的最⼤进程数
默认为200。。。⾮必须设置
;umask=022                  ; 进程创建⽂件的掩码
默认为022。。⾮必须设置项
;ur=chrism                ; 这个参数可以设置⼀个⾮root⽤户,当我们以root⽤户启动supervisord之后。
我这⾥⾯设置的这个⽤户,也可以对supervisord进⾏管理
默认情况是不设置。。。⾮必须设置项
;identifier=supervisor      ; 这个参数是supervisord的标识符,主要是给XML_RPC⽤的。当你有多个
supervisor的时候,⽽且想调⽤XML_RPC统⼀管理,就需要为每个
supervisor设置不同的标识符了
默认是supervisord。。。⾮必需设置
;
directory=/tmp              ; 这个参数是当supervisord作为守护进程运⾏的时候,设置这个参数的话,启动
supervisord进程之前,会先切换到这个⽬录
默认不设置。。。⾮必须设置
;nocleanup=true              ; 这个参数当为fal的时候,会在supervisord进程启动的时候,把以前⼦进程
产⽣的⽇志⽂件(路径为AUTO的情况下)清除掉。有时候咱们想要看历史⽇志,当
然不想⽇志被清除了。所以可以设置为true
默认是fal,有调试需求的同学可以设置为true。。。⾮必须设置
;childlogdir=/tmp            ; 当⼦进程⽇志路径为AUTO的时候,⼦进程⽇志⽂件的存放路径。
默认路径是这个东西,执⾏下⾯的这个命令看看就OK了,处理的东西就默认路径
python -c "import tempfile;pdir()"
⾮必须设置
;environment=KEY="value"    ; 这个是⽤来设置环境变量的,supervisord在linux中启动默认继承了linux的环境变量,在这⾥可以设置supervisord进程特有的其他环境变量。
supervisord启动⼦进程时,⼦进程会拷贝⽗进程的内存空间内容。所以设置的
这些环境变量也会被⼦进程继承。
⼩例⼦:environment=name="haha",age="hehe"
默认为不设置。。。⾮必须设置
;strip_ansi=fal            ; 这个选项如果设置为true,会清除⼦进程⽇志中的所有ANSI 序列。什么是ANSI 序列呢?就是我们的\n,\t这些东西。
默认为fal。。。⾮必须设置
; the below ction must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be蘑菇养殖
; added by defining them in parate rpcinterface: ctions
[rpcinterface:supervisor]    ;这个选项是给XML_RPC⽤的,当然你如果想使⽤supervisord或者web rver 这个选项必须要开启的
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]              ;这个主要是针对supervisorctl的⼀些配置
rverurl=unix:///tmp/supervisor.sock ; 这个是supervisorctl本地连接supervisord的时候,本地UNIX socket
路径,注意这个是和前⾯的[unix_http_rver]对应的
默认值就是unix:///tmp/supervisor.sock。。⾮必须设置
;rverurl=127.0.0.1:9001 ; 这个是supervisorctl远程连接supervisord的时候,⽤到的TCP socket路径
注意这个和前⾯的[inet_http_rver]对应
默认就是127.0.0.1:9001。。。⾮必须项
雨中荷花;urname=chris              ; ⽤户名
默认空。。⾮必须设置
;password=123                ; 密码
默认空。。⾮必须设置
;prompt=mysupervisor        ; 输⼊⽤户名密码时候的提⽰符
默认supervisor。。⾮必须设置
;history_file=~/.sc_history  ; 这个参数和shell中的history类似,我们可以⽤上下键来查找前⾯执⾏过的命令默认是no file的。。所以我们想要有这种功能,必须指定⼀个⽂件。。。⾮
必须设置
; The below sample program ction shows all possible program subction values,
; create one or more 'real' program: ctions to be able to control them under
; supervisor.
;[program:theprogramname]      ;这个就是咱们要管理的⼦进程了,":"后⾯的是名字,最好别乱写和实际进程有点关联最好。这样的program我们可以设置⼀个或多个,⼀个program就是
要被管理的⼀个进程
;command=/bin/cat              ; 这个就是我们的要启动进程的命令路径了,可以带参数
例⼦:/home/test.py -a 'hehe'
有⼀点需要注意的是,我们的command只能是那种在终端运⾏的进程,不能是
守护进程。这个想想也知道了,⽐如说command=rvice httpd start。
httpd这个进程被linux的rvice管理了,我们的supervisor再去启动这个命令
这已经不是严格意义的⼦进程了。
这个是个必须设置的项
;
process_name=%(program_name)s ; 这个是进程名,如果我们下⾯的numprocs参数为1的话,就不⽤管这个参数了,它默认值%(program_name)s也就是上⾯的那个program冒号后⾯的名字,
但是如果numprocs为多个的话,那就不能这么⼲了。想想也知道,不可能每个
进程都⽤同⼀个进程名吧。
;numprocs=1                    ; 启动进程的数⽬。当不为1时,就是进程池的概念,注意process_name的设置
默认为1    。。⾮必须设置
;directory=/tmp                ; 进程运⾏前,会前切换到这个⽬录
默认不设置。。。⾮必须设置
;umask=022                    ; 进程掩码,默认none,⾮必须
;priority=999                  ; ⼦进程启动关闭优先级,优先级低的,最先启动,关闭的时候最后关闭
默认值为999 。。⾮必须设置
;autostart=true                ; 如果是true的话,⼦进程将在supervisord启动后被⾃动启动
默认就是true  。。⾮必须设置
;autorestart=unexpected        ; 这个是设置⼦进程挂掉后⾃动重启的情况,有三个选项,fal,unexpected
和true。如果为fal的时候,⽆论什么情况下,都不会被重新启动,
如果为unexpected,只有当进程的退出码不在下⾯的exitcodes⾥⾯定义的退
出码的时候,才会被⾃动重启。当为true的时候,只要⼦进程挂掉,将会被⽆
条件的重启
;startcs=1                  ; 这个选项是⼦进程启动多少秒之后,此时状态如果是running,则我们认为启
动成功了
默认值为1 。。⾮必须设置
冬季文案
;startretries=3                ; 当进程启动失败后,最⼤尝试启动的次数。。当超过3次后,supervisor将把
此进程的状态置为FAIL
默认值为3 。。⾮必须设置
;exitcodes=0,2                ; 注意和上⾯的的autorestart=unexpected对应。。exitcodes⾥⾯的定义的
退出码是expected的。
;stopsignal=QUIT              ; 进程停⽌信号,可以为TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信号
默认为TERM 。。当⽤设定的信号去⼲掉进程,退出码会被认为是expected
⾮必须设置
;stopwaitcs=10              ; 这个是当我们向⼦进程发送stopsignal信号后,到系统返回信息
给supervisord,所等待的最⼤时间。超过这个时间,supervisord会向该
⼦进程发送⼀个强制kill的信号。
默认为10秒。。⾮必须设置
;stopasgroup=fal            ; 这个东西主要⽤于,supervisord管理的⼦进程,这个⼦进程本⾝还有
⼦进程。那么我们如果仅仅⼲掉supervisord的⼦进程的话,⼦进程的⼦进程
有可能会变成孤⼉进程。所以咱们可以设置可个选项,把整个该⼦进程的
整个进程组都⼲掉。设置为true的话,⼀般killasgroup也会被设置为true。
需要注意的是,该选项发送的是stop信号
默认为fal。。⾮必须设置。。
;killasgroup=fal            ; 这个和上⾯的stopasgroup类似,不过发送的是kill信号
;ur=chrism                  ; 如果supervisord是root启动,我们在这⾥设置这个⾮root⽤户,可以⽤来
管理该program
默认不设置。。。⾮必须设置项
;redirect_stderr=true          ; 如果为true,则stderr的⽇志会被写⼊stdout⽇志⽂件中
默认为fal,⾮必须设置
;stdout_logfile=/a/path        ; ⼦进程的stdout的⽇志路径,可以指定路径,AUTO,none等三个选项。
设置为none的话,将没有⽇志产⽣。设置为AUTO的话,将随机找⼀个地⽅
⽣成⽇志⽂件,⽽且当supervisord重新启动的时候,以前的⽇志⽂件会被
清空。当 redirect_stderr=true的时候,sterr也会写进这个⽇志⽂件
3是什么意思
;stdout_logfile_maxbytes=1MB  ; ⽇志⽂件最⼤⼤⼩,和[supervisord]中定义的⼀样。默认为50
;stdout_logfile_backups=10    ; 和[supervisord]定义的⼀样。默认10
;stdout_capture_maxbytes=1MB  ; 这个东西是设定capture管道的⼤⼩,当值不为0的时候,⼦进程可以从stdout 发送信息,⽽supervisor可以根据信息,发送相应的event。
默认为0,为0的时候表达关闭管道。。。⾮必须项
;stdout_events_enabled=fal  ; 当设置为ture的时候,当⼦进程由stdout向⽂件描述符中写⽇志的时候,将
触发supervisord发送PROCESS_LOG_STDOUT类型的event
默认为fal。。。⾮必须设置
;stderr_logfile=/a/path        ; 这个东西是设置stderr写的⽇志路径,当redirect_stderr=true。这个就不⽤
设置了,设置了也是⽩搭。因为它会被写⼊stdout_logfile的同⼀个⽂件中
默认为AUTO,也就是随便找个地存,supervisord重启被清空。。⾮必须设置
;stderr_logfile_maxbytes=1MB  ; 这个出现好⼏次了,就不重复了
;stderr_logfile_backups=10    ; 这个也是
;stderr_capture_maxbytes=1MB  ; 这个⼀样,和stdout_capture⼀样。默认为0,关闭状态
雷电灾害
;stderr_events_enabled=fal  ; 这个也是⼀样,默认为fal
;environment=A="1",B="2"      ; 这个是该⼦进程的环境变量,和别的⼦进程是不共享的
;rverurl=AUTO                ;
; The below sample eventlistener ction shows all possible
; eventlistener subction values, create one or more 'real'
; eventlistener: ctions to be able to handle event notifications
; nt by supervisor.
;[eventlistener:theeventlistenername] ;这个东西其实和program的地位是⼀样的,也是suopervisor启动的⼦进
程,不过它⼲的活是订阅supervisord发送的event。他的名字就叫
listener了。我们可以在listener⾥⾯做⼀系列处理,⽐如报警等等
楼主这两天⼲的活,就是弄的这玩意
;command=/bin/eventlistener    ; 这个和上⾯的program⼀样,表⽰listener的可执⾏⽂件的路径
;process_name=%(program_name)s ; 这个也⼀样,进程名,当下⾯的numprocs为多个的时候,才需要。否则默认就                                OK了
;numprocs=1                    ; 相同的listener启动的个数
;events=EVENT                  ; event事件的类型,也就是说,只有写在这个地⽅的事件类型。才会被发送
;buffer_size=10                ; 这个是event队列缓存⼤⼩,单位不太清楚,楼主猜测应该是个吧。当buffer
超过10的时候,最旧的event将会被清除,并把新的event放进去。
默认值为10。。⾮必须选项
;directory=/tmp                ; 进程执⾏前,会切换到这个⽬录下执⾏
默认为不切换。。。⾮必须
;umask=022                    ; 淹没,默认为none,不说了
;priority=-1                  ; 启动优先级,默认-1,也不扯了
;autostart=true                ; 是否随supervisord启动⼀起启动,默认true
;autorestart=unexpected        ; 是否⾃动重启,和program⼀个样,分true,fal,unexpected等,注意
unexpected和exitcodes的关系
;startcs=1                  ; 也是⼀样,进程启动后跑了⼏秒钟,才被认定为成功启动,默认1
;startretries=3                ; 失败最⼤尝试次数,默认3
;exitcodes=0,2                ; 期望或者说预料中的进程退出码,
;stopsignal=QUIT              ; ⼲掉进程的信号,默认为TERM,⽐如设置为QUIT,那么如果QUIT来⼲这个进程
那么会被认为是正常维护,退出码也被认为是expected中的
;stopwaitcs=10              ; max num cs to wait b4 SIGKILL (default 10)
;stopasgroup=fal            ; nd stop signal to the UNIX process group (default fal)
;killasgroup=fal            ; SIGKILL the UNIX process group (def fal)
;ur=chrism                  ;设置普通⽤户,可以⽤来管理该listener进程。
默认为空。。⾮必须设置
;redirect_stderr=true          ; 为true的话,stderr的log会并⼊stdout的log⾥⾯
默认为fal。。。⾮必须设置
;stdout_logfile=/a/path        ; 这个不说了,好⼏遍了
;stdout_logfile_maxbytes=1MB  ; 这个也是
;stdout_logfile_backups=10    ; 这个也是
;stdout_events_enabled=fal  ; 这个其实是错的,listener是不能发送event
;stderr_logfile=/a/path        ; 这个也是
;stderr_logfile_maxbytes=1MB  ; 这个也是
;stderr_logfile_backups        ; 这个不说了
;stderr_events_enabled=fal  ; 这个也是错的,listener不能发送event
;
environment=A="1",B="2"      ; 这个是该⼦进程的环境变量
默认为空。。。⾮必须设置
;rverurl=AUTO                ; override rverurl computation (childutils)
; The below sample group ction shows all possible group values,毒瘾
; create one or more 'real' group: ctions to create "heterogeneous"
; process groups.
;[group:thegroupname]  ;这个东西就是给programs分组,划分到组⾥⾯的program。我们就不⽤⼀个⼀个去操作了我们可以对组名进⾏统⼀的操作。注意:program被划分到组⾥⾯之后,就相当于原来
的配置从supervisor的配置⽂件⾥消失了。。。supervisor只会对组进⾏管理,⽽不再
会对组⾥⾯的单个program进⾏管理了
;programs=progname1,progname2  ; 组成员,⽤逗号分开
这个是个必须的设置项
;priority=999                  ; 优先级,相对于组和组之间说的
默认999。。⾮必须选项
; The [include] ction can just contain the "files" tting.  This
; tting can list multiple files (parated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themlves.
;[include]                        ;这个东西挺有⽤的,当我们要管理的进程很多的时候,写在⼀个⽂件⾥⾯
就有点⼤了。我们可以把配置信息写到多个⽂件中,然后include过来
;files = relative/directory/*.ini

本文发布于:2023-05-14 05:57:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/623545.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:进程   默认   设置   启动
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图