[运维开发] 进程管理工具 - supervisor
快速上手
- 安装supervisor
pip install supervisor
- 创建supervisord.conf模板
echo_supervisord_conf > /etc/supervisord/supervisord.conf
- 运行supervisord
supervisord -c /etc/supervisord/supervisord.conf
- 查看supervisor子进程状态
supervisorctl status
核心组件
- supervisord: 这是 Supervisor 的服务端程序,负责启动和管理子进程。它监听配置文件中的指令,根据配置启动和监控进程。supervisord 通常以守护进程(daemon)的方式运行,它在启动时会创建一个或多个子进程,并在子进程退出时自动重启它们(根据配置)。supervisord 的配置文件通常位于 /etc/supervisord.conf,其中定义了如何管理子进程,包括进程的启动命令、日志文件位置、自动重启策略等
- supervisorctl: 这是 Supervisor 的客户端命令行工具,用于与 supervisord 通信。通过 supervisorctl,你可以查看子进程的状态、启动和停止进程、发送信号给进程等。supervisorctl 通过 UNIX socket 或 TCP 连接与 supervisord 通信,通信协议是 XML-RPC。配置通常在 supervisord 配置文件的 [supervisorctl] 部分定义,包括服务器 URL、用户名和密码等信息
- signals: 在 Supervisor 中,signals 指的是可以发送给进程的信号,用于控制进程的行为,如 SIGTERM 用于优雅地停止进程。通过 supervisorctl,你可以发送信号给由 supervisord 管理的进程,例如 supervisorctl signal 15 process_name 会向指定的进程发送 SIGTERM 信号
这些组件共同工作,提供了一个强大的进程管理框架,允许你对多个进程进行集中管理和监控。通过配置文件和命令行工具,你可以灵活地控制进程的生命周期,确保关键服务的高可用性。
supervisord配置文件
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it