我有一个服务foo.service
,它依赖于服务bar.service
。我需要确保 bar.service
在 foo.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.
在同一个单元文件中同时包含 Requires
和 After
部分是否正确? Requires
表示该服务将并行启动,但 After
表示它将在之前启动。如果 bar.service
在 After
条件期间启动失败,它会在 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/