6

I have two services A.service and B.service. I want B to be stopped when A exits/get killed/is stopped. Also, I want B to get restarted when A gets restarted. I tried out the answer given in How to start and stop a systemd unit with another? and set my files up as follows:-

A.service

[Unit]
Description=A
Before=B.service
Requires=B.service
[Service]
ExecStart=/usr/share/A
ExecStopPost=/usr/evo/exit_handler.sh %p
Restart=always
StartLimitBurst=3
StartLimitInterval=300

B.service

[Unit]
Description=B
BindsTo=A.service

[Service]
Type=forking
ExecStart=/usr/share/B start
ExecStop=/usr/share/B stop
StartLimitBurst=5
StartLimitInterval=10
Restart=always

When I kill A or do a systemctl restart A, I see that B gets restarted. But when A exits with status 0, I don't see B getting restarted. I have enabled systemd debug loggin and I see that the state of A changes to dead but not to auto-restart

4

Your B unit is using BindsTo=, which is meant to track the state of a unit that may disappear.

What you want instead of this is PartOf=, where this service will exactly track the status of the named service. From the documentation:

PartOf=
Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.

I don't think this will fix all your problems, though. I suspect you haven't carefully enough considered the actual dependencies between your two services, and will need to do some more thinking (and be able to express yourself) before you can make these units do what they need to do.

  • Thanks! I will try that out. Could you elaborate on these problems though? – Bug Killer Oct 11 '16 at 2:18
  • 1
    Think carefully about which service actually depends on the other, how each behaves in the absence of the other, or when the other crashes. – Michael Hampton Oct 11 '16 at 4:44

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.