Supervisor 管理器

Supervisor是一款用Python开发的进程管理工具,适用于Linux/Unix系统。它能方便地监听、启动、停止和重启进程,并在进程意外终止时自动恢复。本文介绍其安装配置及常见命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

Github https://github.com/Supervisor/supervisor
官方文档 http://supervisord.org/

安装

yum -y install supervisord

配置

服务自启
systemctl enable supervisord.service
启动服务
systemctl start supervisord.service
配置文件
/etc/supervisord.conf
进程配置文件目录
/etc/supervisord.d/

配置进程

新建并添加以下内容(文件扩展名为ini,在配置文件中配置)
vim /etc/supervisord.d/v2ray.ini
[program:v2ray]
command=/usr/local/v2ray/v2ray -config=/usr/local/v2ray/config.json
stdout_logfile=/usr/local/v2ray/supervisor_v2ray.log
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true

管理进程

命令帮助
supervisorctl help
查看进程状态
supervisorctl status
启动进程
supervisorctl start v2ray
重启进程
supervisorctl restart v2ray
停止进程
supervisorctl stop v2ray

配置文件参数说明

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
40
41
42
43
44
[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

; [program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令
autostart=true ; 在supervisord启动的时候也自动启动
startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ; 启动失败自动重试次数,默认是3
user=tomcat ; 用哪个用户启动进程,默认是root
priority=999 ; 进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ; 把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程

;包含其它配置文件
[include]
files = relative/directory/*.ini ;可以指定一个或多个以.ini结束的配置文件

疑难问题

(1) Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting

解决方案:
find / -name supervisor.sock
unlink /run/supervisor/supervisor.sock

Author:  Annda
Link:  https://www.annda.cn/2018/02/02/linux-centos-supervisor/
Copyright Notice:  All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
### 使用 Supervisor 管理和监控 API Six 为了有效地管理与监控 API Six,可以利用 `supervisord` 这一进程控制系统。通过配置文件定义服务启动参数以及日志记录方式等设置,从而实现对 API Six 实例更精细的操作。 #### 安装 Supervisord 对于大多数 Linux 发行版而言,可以通过包管理器安装 supervisord: ```bash sudo apt-get install supervisor # Debian/Ubuntu sudo yum install epel-release # CentOS/RHEL sudo yum install supervisor # CentOS/RHEL ``` 确保已成功安装并启用了该守护程序[^2]。 #### 配置 API Six 作为 Supervised Service 创建一个新的配置文件 `/etc/supervisor/conf.d/apisix.conf` 来描述如何运行 API Six: ```ini [program:apisix] command=/usr/local/bin/apisix start ; 启动命令路径 directory=/path/to/apisix ; 工作目录 (如果适用) autostart=true ; 自动重启选项 startretries=3 ; 尝试重新启动次数 stderr_logfile=/var/log/apisix.err.log ; 错误日志位置 stdout_logfile=/var/log/apisix.out.log ; 输出日志位置 environment=PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" ; 设置环境变量 user=nobody ; 执行身份 stopasgroup=true ; 停止整个进程组 killasgroup=true ; 终止整个进程组 ``` 上述配置项解释如下: - `directory`: 如果有特定的工作目录,则在此处指定; - `autostart`, `startretries`: 控制自动启动行为及其重试机制; - 日志相关字段指定了标准错误流和标准输出流的日志保存地点; - `environment`: 可能需要调整 PATH 或其他必要的环境变量; - `user`: 设定执行此服务的身份,默认为 nobody 用户; - `stopasgroup` 和 `killasgroup`: 当停止或杀死进程时也会影响到其子进程[^1]。 完成以上步骤之后,更新 supervisord 并使新配置生效: ```bash sudo systemctl enable supervisor # 开机自启 sudo service supervisor restart # 重启服务加载新的配置 ``` 此时应该能够看到 API Six 正常由 supervisord 管控,并且可以在 `/var/log/supervisor/` 下找到对应的日志文件以便于后续排查问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值