大网数据广告活动
服务器运维问答

Does systemd fail if a dependency in After=/Requires= doesn't exist?

· 52CE 我爱测

I have a service A that runs in a limited number of environments to provision a host with some files. I have another service B that runs in all environments. I'd like B to depend on (and only start after) A if they are both runni…

问题原文

原帖链接 · Savior · CC BY-SA 4.0

I have a service A that runs in a limited number of environments to provision a host with some files. I have another service B that runs in all environments. I'd like B to depend on (and only start after) A if they are both running in the same environment.

I thought to simply add an After=A and Requires=A to B based on the ​answer here

  • After only checks if the unit is activated already, and does not explicitly activate the specified units.
  • The units listed in Requires are activated together with the unit. If any of the required units fail to start, the unit is not activated.

which seems to indicate that both of these, After= and Requires=, are needed to ensure a dependency is started before this (B) unit.

But what happens if that specified unit doesn't exist, ie. in the environment where service A doesn't run? Is the dependency a no-op?

已采纳解答

原帖链接 · Jeff Schaller · CC BY-SA 4.0

At least as of systemd v219:

  • Adding an After=A to B is harmless when A does not exist; B will still start.
  • Adding a Requires=A to B is mostly harmless when A does not exist; when B is started, you'll get a message in the journal to the effect: "Cannot add dependency job for unit b.service, ignoring: Unit not found." but B will still be started.

Note that the Requires= option ties closely to the Type= option!

  • If service A is a "simple" type (the default), systemd will consider it activated as soon as the ExecStart process is forked off, which is before that service has finished doing its work!
  • If service A is instead a "forking" type, then systemd "will consider the unit started when the parent process exits", which is probably what you want in this case.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-04-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。