I'm trying to create dependencies between two services described in systemd unit files:
- a.service has to start before b.service
- b.service can not start if a.service fails
Here is what I have:
% cat a.service
[Unit]
Description=A-Service
Before=b.service
[Service]
ExecStart=/opt/a.sh
[Install]
WantedBy=multi-user.target
% cat b.service
[Unit]
Description=B-Service
After=a.service
[Service]
ExecStart=/opt/b.sh
[Install]
WantedBy=multi-user.target
And this are simple scripts executed for service A and B:
% cat a.sh
#!/bin/sh
echo "A service started"
exit 0
% cat b.sh
#!/bin/sh
echo "B service started"
while [ true ]; do
sleep 1
do
exit 0
So I've tried to emulate a/service failure by returning exit 1 from a.sh, systemctl status a.service reports it as failed:
Active: failed (Result: exit-code) ...
Process: 843 ExecStart=/opt/a.sh (code=exited, status=1/FAILURE)
...
However b.service still starts and active:
% systemctl status b.service
...
Active: active (running) ...
Process: 844 ExecStart=/opt/b.sh (code=exited, status=0/SUCCESS)
...
Moreover b.service dependencies do contain a.service (with status Fail):
% systemctl list-dependencies b.service
...
What am I doing wrong? Is it a problem with the scripts or [Unit] definition is not complete?