介绍

  • 演示环境:uname - alsb_release -a

    image-20230328092321566

  • apt安装为例,更多说明查看官网

  • pip安装配置比较麻烦,建议用系统级安装方式

  • 安装路径:/etc/supervisor

安装

  1. 安装supervisor

    1
    2
    sudo apt-get update
    sudo apt-get install supervisor
  2. 验证是否安装完成,输入echo_supervisord_conf,有如下输入即说明安装成功

    image-20230327181942590

  3. 配置,生成默认配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    # 创建配置文件夹,默认连/etc/supervisor都没有(这个可以先查一下,直接用pip安装没有)
    sudo mkdir -p /etc/supervisor/conf.d

    # 新建配置文件,官方做法 echo_supervisord_conf > /etc/supervisor/supervisord.conf 不推荐,太多不需要的
    # 手动创建以下文件或路径,否则将会报错,无法正常使用
    sudo mkdir /var/log/supervisor

    # 修改配置文件
    sudo vim /etc/supervisor/supervisord.conf

    # 填入一下内容后保存退出
    ; 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
  4. 执行配置文件:sudo supervisord -c supervisord.conf,如果启动报错Error: Another program is already listening on a port that one of our HTTP servers is configured to use,使用如下方法解决

    1
    2
    3
    4
    # 现在supervisord进程
    ps -ef | grep supervisord
    # 杀死进程
    kill -s SIGTERM [进程号]

    image-20230328093716573

  5. 创建子进程配置文件,示例如下

    1
    2
    3
    4
    5
    6
    [program:Hello]
    command=/usr/bin/python3 /opt/hello_test.py
    autostart=true
    autorestart=true
    user=root
    stopasgroup=true
  6. 启动管控:sudo supervisorctl update,完成

配置文件说明

主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[unix_http_server]
file=/tmp/supervisor.sock #UNIX socket 文件,supervisorctl 会使用
chmod=0700 #socket文件的mode,默认是0700
chown=nobody:nogroup #socket文件的owner,格式:uid:gid

[inet_http_server] #HTTP服务器,提供web管理界面
port=127.0.0.1:9001 #Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性
username=user #登录管理后台的用户名
password=123 #登录管理后台的密码

[supervisord]
logfile=/tmp/supervisord.log #日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB #日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10 #日志文件保留备份数量默认10,设为0表示不备份
loglevel=info #日志级别,默认info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid #pid 文件
nodaemon=false #是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024 #可以打开的文件描述符的最小值,默认 1024
minprocs=200 #可以打开的进程数的最小值,默认 200

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock #通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
serverurl=http://127.0.0.1:9001 #通过HTTP的方式连接supervisord

#包含其它配置文件
[include]
files = relative/directory/*.ini #可以指定一个或多个以.ini结束的配置文件
# 也可以指定其他后缀,如
files = /etc/supervisor/conf.d/*.conf

子进程配置文件

  • /etc/supervisor/conf.d/,子进程test.py为例,创建/etc/supervisor/conf.d/blog.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    ########################## 基本配置,配置后就可用 ##########################
    # 项目名
    [program:blog]
    # 脚本目录(非必须)
    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=true
    # 脚本运行的用户身份
    user=root
    # 这个东西主要用于,supervisord管理的子进程,这个子进程本身还有子进程。
    # 么我们如果仅仅干掉supervisord的子进程的话,子进程的子进程有可能会变
    # 孤儿进程。所以咱们可以设置可个选项,把整个该子进程的整个进程组都干掉。设置为
    # true的话,一般killasgroup也会被设置为true。需要注意的是,该选项
    # 送的是stop信号默认为false。非必须设置。
    stopasgroup=true

    ########################## 额外配置 ##########################

    #这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
    startsecs=1

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

使用

supervisor重启

  • 验证supervisor是否挂掉:sudo supervisorctl,输入密码后发现执行失败,说明supervisor服务挂了
  1. 首先找到supervisor安装的目录:whereis supervisor
  2. 找到对应的supervisor配置文件路径,如/etc/supervisor/supervisord.conf
  3. 启动supervisor服务:sudo supervisord -c /etc/supervisor/supervisord.conf
  4. 查看supervisor服务是否正常运行:sudo supervisorctl

supervisor进程管理

  • 权限不够加sudo,由配置文件中user身份决定

  • 也可以直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用

    image-20230226112126903

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 查看所有进程的状态
    supervisorctl status
    # 启动进程
    supervisorctl start [进程名]
    # 停止进程
    supervisorctl stop [进程名]
    # 重启进程
    supervisorctl restart [进程名]
    # 配置文件修改后使用该命令加载新的配置
    supervisorctl update
    # 重新启动配置中的所有程序
    supervisorctl reload

参考

  1. Supervisor使用详解
  2. Supervisor 配置详解
  3. 重启supervisor服务及 查看、启动、停止、重启supervisor进程
  4. supervisorctl命令的使用讲解
  5. Supervisor的作用与配置