1

example:

bar.service:

[Unit]
After=foo.service

if foo.service isn't started correctly, will bar.service start? and what about if foo.service isn't a dependency of anything and will never start?

muru
  • 72,889
AleD219
  • 11
  • 2

1 Answers1

1

You should view After=x as meaning:

  • Start doing this job after you have finished doing job "x".
  • It doesn't instruct anything with respect to the result of doing job "x", it only instructs to wait with this job until job "x" has been performed.

From man systemd.unit:

Most importantly, for service units start-up is considered completed for the purpose of Before=/After= when all its configured start-up commands have been invoked and they either failed or reported start-up success. Note that this does includes ExecStartPost= (or ExecStopPost= for the shutdown case).

Note that those settings are independent of and orthogonal to the requirement dependencies as configured by Requires=, Wants=, Requisite=, or BindsTo=. It is a common pattern to include a unit name in both the After= and Wants= options, in which case the unit listed will be started before the unit that is configured with these options.

So that means:

  1. bar.service WILL start even if foo.service ends with a failed state.
  2. If foo.service is never activated, then bar.service will stay waiting until foo.service is activated somehow by other units.
  3. bar.service will most likely eventually enter a failed state due to unmet dependency.
    The time that is used to wait is controlled by other settings like JobTimeoutSec=, JobRunningTimeoutSec=. (See: How to change systemd service timeout value?)
    You could use JobTimeoutAction= to catch this case.
  • EDIT: Scrapped (2) and (3) because those only apply if Requisite=foo.service, thanks for correcting me @TooTea
  • 1
    Point #2 is incorrect. Before/After is just an ordering relation, not a dependency. The ordering only matters if both units involved are being started/stopped together (in a single transaction), so After=foo.service simply does nothing if foo.service is never started. That's why you typically combine After with Requires(Wants); the latter triggers an activation of the unit you depend on, and After then takes care of the relative ordering of these two units. – TooTea Sep 05 '23 at 09:42