23

The definition given in the man for systemd unit is a bit unclear: https://www.freedesktop.org/software/systemd/man/systemd.unit.html

If a unit foo.service contains a setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service is started up. [...] After= is the inverse of Before=, i.e. while After= ensures that the configured unit is started after the listed unit finished starting up, Before= ensures the opposite, that the configured unit is fully started up before the listed unit is started.

Lets say I have a.service and b.service. I want a.service to start up completely before b.service because b.service depends on a.service.

After reading the aforementioned man page I couldn't find any conclusive explanation on whether:

  • You only need to specify Before=b.service in the a.service unit file
  • You only need to specify After=a.service in the b.service unit file
  • You need both After=a.service in the b.service unit file and Before=b.service in the a.service unit file

Which do I need to declare dependencies for systemd unit files? Does it matter?

6
  • If b depends on a, aren't you looking for Wants or Requires or Requisite anyway? Jan 25, 2018 at 20:10
  • Have you tried experimenting with your units’ declarations? Jan 25, 2018 at 20:10
  • @Ulrich Schwarz I do have Requires as I need a hard fail for b if a hasn't started. However according to the aforementioned man page: ` If a unit foo.service requires a unit bar.service as configured with Requires= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated. `
    – Wimateeka
    Jan 25, 2018 at 20:13
  • @Stephen Kitt I haven't tried experimenting yet as I am translating a large project which includes numerous upstart jobs to systemd services and they all have many dependencies. Give me a few weeks and I will get back to you ;)
    – Wimateeka
    Jan 25, 2018 at 20:14
  • What I meant was that you could answer your own question with a simple experiment involving your two a and b example units; no need to wait until you’ve finished translating all your upstart jobs! Jan 25, 2018 at 20:43

1 Answer 1

23

You only need one of After= or Before= in your pair of units. You might prefer this from the man page for systemctl:

--after ... any After= dependency is automatically mirrored to create a Before= dependency.

Use this option with list-dependencies to check what you think systemd should be doing. Eg

$ systemctl list-dependencies --after timers.target
timers.target
* |-sysstat-collect.timer
* |-sysstat-summary.timer
* |-systemd-tmpfiles-clean.timer
* `-unbound-anchor.timer

$ systemctl list-dependencies --before sysstat-collect.timer
sysstat-collect.timer
* |-sysstat-collect.service
* |-shutdown.target
* `-timers.target

If you are converting from upstart you might get some hints from here, and you could read all the blogs listed here under the heading The systemd for Administrators Blog Series.

You must log in to answer this question.

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