问题原文
原帖链接 · 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
Afteronly checks if the unit is activated already, and does not explicitly activate the specified units.- The units listed in
Requiresare 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=Ato B is harmless when A does not exist; B will still start. - Adding a
Requires=Ato 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
