3

How can I start a service unit only if another service unit runs without any errors?

I have 2 service units:

#echo-date-0.service
[Unit]
Description=

[Service]
ExecStart=/home/user/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target



#echo-date-1.service
[Unit]
Description=

[Service]
ExecStart=/home/user/bash/echo-date-1.sh
Requires=echo-date-0.service
After=echo-date-0.service

[Install]
WantedBy=multi-user.target

I have the script echo-date-0.sh returning exit code 1 (exit 1), and if I check the status of echo-date-0.service I see:

Active: failed (Result:exit-code) 
Process: (code=exited, status=1/FAILURE)

But, echo-date-1.service runs even though I have it Requiring echo-date-0.service. How can I stop echo-date-1.service from running, if echo-date-0.service fails?

  • 1
    I think you should use BindsTo instead of Requires. freedesktop.org/software/systemd/man/systemd.unit.html – Jesse_b Jul 20 '17 at 16:21
  • is there a limit to how many services you can "chain" with BindsTo= ? I have 4 services which im trying to "chain" with BindsTo= and the last service does not start. The last service is the one that is enabled, which then starts all the other services. It is the same script as this is for testing. So I have echo-date-0-3. I have echo-date-3 enabled but it does not run. Only echo-date-0-2 run. If i start echo-date-3.service manually, it runs without errors. – jes516 Jul 20 '17 at 18:14
  • Or how about this, can I start more than 1 service if a service runs successfully? Meaning, if echo-date-0.service runs and is a success, then start echo-date-1,2,3 – jes516 Jul 20 '17 at 18:21
0

Requires= and After= need to be in the [Unit] section

0
#echo-date-0.service
[Unit]
Description=

[Service]
Type=oneshot
## Stay alive for other services to acknowledge 
RemainAfterExit=yes
ExecStart=/home/user/bash/echo-date-0.sh

[Install]
WantedBy=multi-user.target



#echo-date-1.service
[Unit]
Description=
Requires=echo-date-0.service
After=echo-date-0.service
## A unit that must be in an active non-erroring state
## and combos great with After=
BindsTo=echo-date-0.service

[Service]
ExecStart=/home/user/bash/echo-date-1.sh

[Install]
WantedBy=multi-user.target

Jesse_b was, at least for me, correct in mentioning BindsTo= within the systemd.unix would be useful.

And Bigon is correct in stating that the [Unit] section should have Requires= & After=.

Effectively echo-date-0.service, in the above (and hopefully functioning) example, remains alive as a service (so running systemctl stop echo-date-0.service is required to reset state), but by setting things up so and combining After= & BindsTo= within echo-date-1.service pointing back to the service that must not error, this should achieve what the OP is asking for here.

One should be able to run systemctl list-dependencies --before <service/trigger> on echo-date-0.service in this case, to view services that linked to it in one form or another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.