如何对 systemd 服务进行分组/组合?

Kim*_*ble 7 systemd

我有三个 systemd 服务(Docker 容器,但这并不重要)。所有这三个服务都必须运行,应用程序才能完全正常运行。

为了简化操作团队的程序,我创建了第四个 systemd 服务,使用RequiresandPartOf指令将它们组合在一起。启动和停止这第四个服务现在将以正确的顺序启动和停止三个容器。

最后一个挑战是让第四个 systemd 服务显示正确的状态。现在它在启动服务后显示“加载/非活动”。是否可以将其配置为当且仅当三个第一个服务启动并运行时才显示“已加载/活动”?

[Unit]
Description=This is the group
Requires=a.service b.service c.service

[Service]
ExecStart=/bin/echo "Starting"
ExecStop=/bin/echo "Stopping"

[Install]
WantedBy=local.target
Run Code Online (Sandbox Code Playgroud)

a.serviceb.servicec.service含有PartOf=group.service

iwa*_*rue 1

我认为你需要添加一个After=条款。如果您的三项服务是链接的,您也许还可以简化您的Requires.

如果您还希望第四个服务在其他 3 个服务停止时停止,则还需要添加一个BindsTo条目。

如:

[Unit]
Description=This is the group
Requires=c.service
After=c.service
BindsTo=c.service
Run Code Online (Sandbox Code Playgroud)