linux - Systemd:同时使用 After 和 Requires

标签 linux redhat systemd

我有一个服务foo.service,它依赖于服务bar.service。我需要确保 bar.servicefoo.service 之前启动并且 bar.service 已成功启动。

来自 this source它说 Requires:

This directive lists any units upon which this unit essentially depends. If the current unit is activated, the units listed here must successfully activate as well, else this unit will fail. These units are started in parallel with the current unit by default.

之后:

The units listed in this directive will be started before starting the current unit. This does not imply a dependency relationship and one must be established through the above directives if this is required.

在同一个单元文件中同时包含 RequiresAfter 部分是否正确? Requires 表示该服务将并行启动,但 After 表示它将在之前启动。如果 bar.serviceAfter 条件期间启动失败,它会在 Requires 部分尝试再次启动它吗?如果是这样,我需要找到另一种方式来启动 foo.service

foo.service

[Unit]
After=bar.service
Requires=bar.service

最佳答案

同时使用 After=Requires= 是完全没问题的。他们有不同的目的。 Requires= 设置启动依赖项。 systemd 确保如果任何主体试图启动 foo.service,它也应该启动 bar.service。如果 bar.service 在某个时候失败,那么 foo.service 也会被关闭。

After= 在服务之间放置启动顺序。如果这两个服务都计划启动,则 After= 确保设置启动顺序。

可以以systemd自带的服务文件为例。

/lib/systemd/system/basic.target
[Unit]
...
Requires=sysinit.target
After=sysinit.target
...

关于linux - Systemd:同时使用 After 和 Requires,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593242/

相关文章:

c - 如何将 PCM 数据转换为十六进制数据

c - 从以前的 libc.so.6 库调用访问 errno 值的方法?

linux - 分发构建版本的 gcc

firefox - 为什么 firefox 不会在 redhat 5.6 上的 selenium 2 webdriver 下启动

service - 在特定服务之后启动systemd服务?

python - 使用 Gunicorn 部署 Django 应用程序时出错?

c - 如何使POSIX/Linux信号处理安全?

Azure 上的 Linux VM 控制台

linux - linux更新环境变量后anaconda无法识别

unix - 从 C/C++ 应用程序启动 systemd 服务或调用 D-Bus 服务