Skip to main content
added 194 characters in body
Source Link
Mark
  • 1.9k
  • 6
  • 32
  • 52

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: inactiveactive (deadrunning) ...
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?

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"
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:

% systemctl status b.service
...
Active: inactive (dead) ...
Process: 844 ExecStart=/opt/b.sh (code=exited, status=0/SUCCESS)
...

What am I doing wrong? Is it a problem with the scripts or [Unit] definition is not complete?

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?

Source Link
Mark
  • 1.9k
  • 6
  • 32
  • 52

systemd unit file: dependencies "After=/Before=" don't work

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"
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:

% systemctl status b.service
...
Active: inactive (dead) ...
Process: 844 ExecStart=/opt/b.sh (code=exited, status=0/SUCCESS)
...

What am I doing wrong? Is it a problem with the scripts or [Unit] definition is not complete?