概述
Supervisord
是一个轻量级的进程管理工具。它允许创建和管理多个进程,并提供一些高级功能,如进程监控、自动重启、日志管理等。
他和 Systemd
一样,都是用于管理进程的工具。
安装
1 | apt install supervisor |
安装完成后有个配置文件: supervisord.conf
如果找不到的话用 find 寻找一下就行.
然后需要启动 supervisord
服务:
1 | supervisord -c /etc/supervisor/supervisord.conf |
注意下,这里的配置文件是你实际的配置文件所在路径,如果你移动过这个 conf 文件的位置,请使用实际的路径.
进行配置
和上一篇文章一样继续假设我们的应用叫 project-apps
并且需要创建一个服务单元.
给supervisord
添加配置:
1 | [supervisord] |
逐项解释:
[supervisord] 部分:
nodaemon=true
:以非守护进程模式运行(容器需要前台进程)。logfile=/dev/null
:禁用 supervisord 自身的日志文件(容器日志通过 stdout/stderr 收集)。logfile_maxbytes=0
:防止日志文件增长。pidfile=/tmp/supervisord.pid
:指定 PID 文件路径。
[program:project-apps] 部分:
command=/usr/local/bin/project-apps
:运行的命令,等价于 systemd 的 ExecStart。user=nobody
:以 nobody 用户运行,等价于 systemd 的 User=nobody。environment=PATH=...
:设置环境变量,等价于 systemd 的 Environment。autostart=true
:supervisord 启动时自动启动 project-appsautorestart=true
:进程退出时自动重启,等价于 systemd 的 Restart=always。startsecs=10
:进程运行 10 秒后认为启动成功(避免频繁重启)。startretries=3
:启动失败时重试 3 次。redirect_stderr=true
:将 stderr 重定向到 stdout。stdout_logfile=/dev/stdout
:将 stdout 输出到容器日志。stderr_logfile=/dev/stderr
:将 stderr 输出到容器日志。
启动
1 | supervisord -c /etc/supervisor/supervisord.conf |
查看状态
1 | supervisorctl status |
停止
1 | supervisorctl stop project-apps |
重启
1 | supervisorctl restart project-apps |
支持的各个参数
参数名 | 描述 | 类型 | 默认值 | 示例值 | 与 systemd 的对应 |
---|---|---|---|---|---|
command |
指定要运行的命令或可执行文件路径 | 字符串 | 无(必填) | project-apps |
ExecStart |
user |
指定运行进程的用户 | 字符串 | 无(以 supervisord 的用户运行) | nobody |
User |
environment |
设置进程的环境变量,格式为 KEY=”value”,KEY2=”value2” | 字符串 | 无 | PATH="/usr/local/bin:/usr/bin:/bin" |
Environment |
directory |
运行命令前切换的工作目录 | 字符串 | 无(当前目录) | /app |
WorkingDirectory |
autostart |
是否在 supervisord 启动时自动启动进程 | 布尔值 | true | true |
WantedBy(间接对应) |
autorestart |
进程退出时是否自动重启(true/false/unexpected) | 字符串 | false | true |
Restart(always 对应 true) |
startsecs |
进程运行多少秒后认为启动成功(用于重启判断) | 整数 | 1 | 10 |
无直接对应(类似 RestartSec) |
startretries |
启动失败时的最大重试次数 | 整数 | 3 | 3 |
无直接对应 |
exitcodes |
视为正常退出的退出码(仅当 autorestart=unexpected 时生效) | 逗号分隔的整数 | 0 | 0,2 |
无直接对应 |
stopwaitsecs |
停止进程时等待的秒数(超时后发送 SIGKILL) | 整数 | 10 | 10 |
TimeoutStopSec |
stopasgroup |
是否向整个进程组发送停止信号 | 布尔值 | false | true |
无直接对应(类似 KillMode=process-group) |
killasgroup |
是否向整个进程组发送终止信号(SIGKILL) | 布尔值 | false | true |
无直接对应 |
stopsignal |
用于停止进程的信号 | 字符串 | TERM | INT |
KillSignal |
priority |
进程启动的优先级(数字越小优先级越高) | 整数 | 999 | 100 |
无直接对应(类似 Nice 或启动顺序) |
numprocs |
启动的进程实例数(多实例运行) | 整数 | 1 | 2 |
无直接对应 |
numprocs_start |
多实例进程的起始编号(与 numprocs 配合) | 整数 | 0 | 1 |
无直接对应 |
process_name |
进程名称模板(用于多实例,含 % 占位符) | 字符串 | %(program_name)s | %(program_name)s_%(process_num)02d |
无直接对应 |
redirect_stderr |
是否将 stderr 重定向到 stdout | 布尔值 | false | true |
无直接对应(日志处理相关) |
stdout_logfile |
stdout 日志输出文件路径(支持 /dev/stdout) | 字符串 | AUTO(自动生成日志文件) | /dev/stdout |
无直接对应(systemd 使用 journalctl) |
stdout_logfile_maxbytes |
stdout 日志文件的最大大小 | 字符串 | 50MB | 10MB |
无直接对应 |
stdout_logfile_backups |
stdout 日志文件的备份数量 | 整数 | 10 | 5 |
无直接对应 |
stderr_logfile |
stderr 日志输出文件路径 | 字符串 | AUTO | /dev/stderr |
无直接对应 |
stderr_logfile_maxbytes |
stderr 日志文件的最大大小 | 字符串 | 50MB | 10MB |
无直接对应 |
stderr_logfile_backups |
stderr 日志文件的备份数量 | 整数 | 10 | 5 |
无直接对应 |
stdout_capture_maxbytes |
stdout 捕获缓冲区大小(用于事件监听) | 字符串 | 0 | 1MB |
无直接对应 |
stdout_events_enabled |
是否启用 stdout 事件(用于事件监听) | 布尔值 | false | true |
无直接对应 |
stderr_events_enabled |
是否启用 stderr 事件 | 布尔值 | false | true |
无直接对应 |