系统学习 Linux 学习路径:从文件与进程到排障与安全

supervisord

用配置文件管理和守护普通进程,并提供 supervisorctl 控制接口。

补充说明

Supervisor 是 Python 写的进程管理器:supervisord 作为守护进程启动子程序,按 autorestart 重启退出进程,把 stdout/stderr 写入日志,并通过 Unix socket/XML-RPC 暴露给 supervisorctl。它适合管理应用进程,但不是 init 系统,不能替代 systemd 处理开机顺序和系统级依赖。

配置为 INI:[program:name] 定义 command、directory、user、environment、autostart、autorestart、stdout_logfile;[unix_http_server] 和 [supervisorctl] 定义控制通道。程序必须以非守护方式前台运行,否则 supervisor 会认为它启动后立即退出。

常用示例

# 前台启动便于排错,指定配置文件
supervisord -n -c /etc/supervisord.conf

# 查看状态、重载配置并应用变更
supervisorctl -c /etc/supervisord.conf status
supervisorctl -c /etc/supervisord.conf reread
supervisorctl -c /etc/supervisord.conf update

# 重启单个程序并跟踪日志
supervisorctl restart myapp
supervisorctl tail -f myapp

易错点与排错

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. 修改了 [program:name] 配置后只执行 restart,为什么新配置没有生效?
  2. 子进程反复重启并报 “exited too quickly”,最常见的原因是什么?
  3. supervisor 启动的子进程找不到命令,应如何解决?
答案
  1. restart 不会重新读取配置文件,必须先 reread 再 update。
  2. command 自行 daemonize 了,supervisor 会认为它启动后立即退出;应去掉 --daemon/-d,让程序前台运行。
  3. 子进程的 PATH 很短,应使用绝对路径或显式设置 environment。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令