linux · 2021-09-29 0

supervisor的使用

一、概述

Supervisor是一个用 Python 写的进程管理工具,可以很方便的用来启动、重启、关闭进程。除了对单个进程的控制,还可以同时启动、关闭多个进程。

Supervisor是一个C/S系统,supervisord是服务端,它负责调用自己启动子程序,该服务的配置文件在/etc/supervisor/supervisord.conf,supervisorctl是客户端的命令行工具,提供一个类shell接口,通过它可以连接到不同的supervisord进程上来管理它们各自的子程序。

二、安装及配置

1.安装

ubuntu系统可使用apt安装supervisor

apt install supervisor

2.配置

supervisor的配置文件在/etc/supervisor/supervisord.conf

inet_http_server的设置,用于web访问

如下:

; 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

[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001        ; (ip_address:port specifier, *:port for all iface)
; username=admin             ; (default is no username (open server))
; password=123456            ; (default is no password (open server))

; 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

3.项目配置

新建文件 /etc/supervisor/conf.d/p.conf

  • process_name 进程名称
  • command 启动进程的命令,只能是那种在终端运行的进程,不能是守护进程
  • autostart 如果是true的话,子进程将在supervisord启动后被自动启动。默认就是true。
  • autorestart 设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的退出码的时候,才会被自动重启。当为true的时候,只要子进程挂掉,将会被无条件的重启。
  • numprocs 启动进程的数目,默认为1
  • exitcodes 意和上面的的autorestart=unexpected对应。exitcodes里面的定义的退出码是expected的。

如下:

[program:pingp]
process_name=%(program_name)s_%(process_num)02d
command=/bin/ping localhost
autostart=false
autorestart=false
numprocs=1
exitcodes=0,2

三、命令

1.启动supervisor服务

supervisord -c /etc/supervisor/supervisord.conf

2.重启supervisor

supervisorctl reload

3.查看supervisor进程

supervisorctl status

4.启动某个supervisor进程

supervisorctl start <program_name>

4.停止某个supervisor进程

supervisorctl stop <program_name>

5.重启某个supervisor进程

supervisorctl restart <program_name>

6.停止所有supervisor进程

supervisorctl stop all

可以在web界面查看进程

http://127.0.0.1:9001/