3

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?

1 Answer 1

4

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.
1
  • 1
    For short "provisioning" scripts, Type=oneshot would be the preferred choice, behaving somewhat like Type=forking but actually meant to go directly back to inactive when the script is done. Using Type=forking with something that doesn't leave a daemon running is a bit of a weird hack. Mar 18, 2022 at 6:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .