apt-get install -y supervisor
Description: System for controlling process state
Supervisor is a system for controlling and maintaining process state,
similar to what init does, but not intended as an init replacement.
bash
root@debian:~ ⍉
▶ apt info supervisor
Package: supervisor
Version: 4.2.5-1
Priority: optional
Section: admin
Maintainer: Debian Python Team <[email protected]>
Installed-Size: 1,748 kB
Pre-Depends: init-system-helpers (>= 1.54~)
Depends: lsb-base, python3-pkg-resources, python3:any
Suggests: supervisor-doc
Homepage: http://supervisord.org/
Tag: implemented-in::python, role::program
Download-Size: 311 kB
APT-Manual-Installed: yes
APT-Sources: http://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 Packages
Description: System for controlling process state
Supervisor is a system for controlling and maintaining process state,
similar to what init does, but not intended as an init replacement.
.
It will manage individual processes or groups of processes that
need to be started and stopped in order, and it is possible to
control individual process state via an rpc mechanism, thus allowing
ordinary users to restart processes.
It will manage individual processes or groups of processes that need to be started and stopped in order,
and it is possible to control individual process state via an rpc mechanism,
thus allowing ordinary users to restart processes.
1. 使用参考
将其作为包安装使我们能够将其视为服务:
service supervisor start
1.1 配置
Supervisord 的配置位于 /etc/supervisor
中。如果我们看一下 配置文件 /etc/supervisord/supervisord.conf
,我们将在底部看到以下内容:
bash
[include] files = /etc/supervisor/conf.d/*.conf
因此,任何在 /etc/supervisor/conf.d
中找到并以 .conf
结尾的文件都将被包括在内。我们可以在此处为我们的服务添加配置。
现在我们需要告诉 Supervisord 如何运行和监控我们的 Node 脚本。我们要做的是创建一个配置,告诉 Supervisord 如何启动和监控 Node 脚本。
让我们为它创建一个名为 webhooks.conf
的配置。此文件将在 以下位置 /etc/supervisor/conf.d/webhooks.conf
创建:
ini
[program:nodehook]
command=/usr/bin/node /srv/http.js # 监控这个进程(控制:启动|关闭)
directory=/srv
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/webhook/nodehook.err.log
stdout_logfile=/var/log/webhook/nodehook.out.log
user=www-data
environment=SECRET_PASSPHRASE='this is secret',SECRET_TWO='another secret'
[program:nodehook]
- 定义要监控的程序。我们称之为 "nodehook"。command
- 这是要运行的命令,用于启动受监视的进程。我们使用 "node" 并运行 "http.js" 文件。如果您需要传递任何命令行参数或其他数据,可以在此处执行此作。directory
- 在运行进程之前,将 Supervisord 的目录设置为 "cd" into,这在进程采用相对于已执行脚本位置的目录结构的情况下非常有用。autostart
- 设置此 "true" 意味着进程将在 Supervisord 启动时启动(主要是在系统启动时)。autorestart
- 如果为 "true",则程序将在意外退出时重新启动。startretries
- 在将进程视为"失败"之前要执行的重试次数stderr_logfile
- 用于写入任何错误输出的文件。stdout_logfile
- 要写入任何常规输出的文件。user
- 运行进程的用户。environment
- 要传递给进程的环境变量。
1.2 控制进程
现在我们已经配置了 Supervisord 来监控我们的 Node 进程,我们可以使用 supervisorctl
工具读入配置,然后重新加载 Supervisord:
sql
supervisorctl reread
supervisorctl update
我们的 Node 进程现在应该正在运行。我们可以通过简单地运行 supervisorctl
来检查这一点:
bash
$ supervisorctl
nodehook RUNNING pid 444, uptime 0:02:45
我们可以使用 ps
命令仔细检查这一点:
bash
$ ps aux | grep node
www-data 444 0.0 2.0 659620 10520 ? Sl 00:57 0:00 /usr/bin/node /srv/http.js
它正在运行!如果我们在端口 9000 上检查本地主机,我们将看到 NodeJS 脚本写出的输出,包括环境变量。如果我们需要将信息或凭证传递给我们的脚本,环境变量非常有用。
如果您的进程没有运行,请尝试通过 supervisorctl start nodehook
显式告诉 Supervisord 启动进程 "nodehook"
我们还可以使用 supervisorctl
做其他事情。使用 supervisorctl
输入控制工具:
bash
$ supervisorctl
nodehook RUNNING pid 444, uptime 0:15:42
我们可以尝试更多命令:
获取可用命令的菜单:
shell
supervisor> help
停止这个进程:
vbnet
supervisor> stop nodehook
nodehook: stopped
然后我们可以重新启动它
makefile
supervisor> start nodehook
nodehook: started
我们可以使用 <ctrl+c> 或键入 "exit" 来退出 supervisorctl 工具。
这些命令也可以直接运行:
ruby
$ supervisorctl stop nodebook
$ supervisorctl start nodebook
1.3 Web Interface Web 界面
我们可以配置 Supervisord 附带的 Web 界面。这使我们可以看到所有被监控进程的列表,并对它们采取措施(重新启动、停止、清除日志和检查输出)。
在 /etc/supervisord.conf
中,添加以下内容:
ini
[inet_http_server]
port = 9001
username = user # Basic auth username
password = pass # Basic auth password
如果我们在 Web 浏览器中通过端口 9001 访问我们的服务器,我们将看到 Web 界面: 单击进程名称(在本例中为"nodehook")将显示该进程的日志。
2. init vs systemd vs supervisor
init
进程和 supervisor
是两种不同的进程管理工具,各自有其特定的功能和用途。以下是对它们的比较和解释:
init
进程
-
基本功能:
init
是 Unix 和 Linux 系统中最初的用户空间进程,进程 ID (PID) 为 1。它负责启动系统,在内核启动后负责加载系统的其他进程和服务。init
主要用于启动系统服务、处理关机和重启过程,并维护系统进程的基本状态。
-
进程管理:
init
进程可以管理基本的进程生命管理(如启动、停止服务),但是控制和监视进程状态的能力相对有限。对程序的重启通常需要手动或配置重启选项。- 大多数现代 Linux 发行版中的
systemd
是init
进程的一种替代品,提供更多的功能(如并行启动、服务依赖关系管理等)。
supervisor
-
基本功能:
supervisor
是一种进程控制系统,旨在更好地管理和维护后台进程的状态。它提供了比传统init
或systemd
更高级的功能。supervisor
可以方便地启动、停止和重启进程,并能够监控进程状态,当进程崩溃时可以自动重启。
-
特性:
supervisor
提供了一个用于管理多个进程的界面。您可以轻松地配置进程,设置其依赖关系,并可使用 RPC 机制控制这些进程。- 它具有更直观的配置文件管理,允许普通用户操作进程,不需要 root 权限。
- 提供了命令行工具和 Web 界面,使得管理后台进程变得更加简单。
为什么需要 supervisor
- 复杂性管理 : 随着系统服务数量的增加,使用
init
或systemd
直接管理所有服务变得麻烦。supervisor
可以将进程管理集中化,方便管理多个后台服务。 - 用户友好 : 对于希望以简单方式管理其应用程序的开发者,
supervisor
使得监控进程状态变得更加方便且灵活。 - 开发环境支持 : 在开发环境中,开发者经常需要管理多进程应用(如调用 Web 服务器和数据库服务器)。
supervisor
使得在本地测试这些服务变得更加简单。
总结
虽然 init
进程提供了基础的系统启动和服务管理功能,但 supervisor
提供了更多特性和灵活性,尤其是在多进程和复杂服务管理的场景下。根据应用程序或系统的需要,开发者和管理员可以选择使用其中一种或两种工具。