Reputation: 169
(systemd version 229)
I have a primary service A, and a secondary service B. The primary A can run by itself. But service B cannot run correctly by itself: it needs A to be running (technically B can run, but this is what I want systemd
to prevent). My goal: If A is not running, B should not run. Given that A and B are running, when A stops or dies/crashes, then B should be stopped.
How do I achieve this?
I get close by adding [Unit] items to b.service, using
Requisite=A.service
After=A.service
The result of the above is that
How can I fix this last behavior? Neither PartOf
nor BindsTo
seems to do the trick, but perhaps I don't have the right incantation of combinations of options? Its not clear to me from the man pages what options can be combined.
systemd.unit man page: https://www.freedesktop.org/software/systemd/man/systemd.unit.html Related: Systemctl dependency failure, stop dependent services
Upvotes: 6
Views: 8461
Reputation: 155
You can use, Requires=
or PartOf=
or BindTo=
see this article for detail of their usage
Upvotes: 3
Reputation: 3605
If you start Service A with Type=notify
you may be able to achieve something if you terminate A with SIGINT
or SIGTERM
, you can actually handle that and send a message on $NOTIFY_FD
to systemd
, but that option is still not possible with SIGKILL
. It's a bit involved but might be able to achieve what you want.
You should also consider making A Restart=always
. This will at-least make sure that A will remain available and B won't keep giving out errors. When you kill
A (outside systemd
), there's no way for systemd
to know that A was killed - especially if you do so with kill -9
(SIGKILL
cannot be handled). . So one of the best way to handle that will be to make Service A, Restart=always
.
Upvotes: 2
Reputation: 667
To achieve your 3rd objective make use of PartOf
keyword.
In B.service you need to add dependency on A under [Unit]
section as below
[Unit]
..
..
PartOf=A.service
With this, whenever A is killed B shall also stop.
Upvotes: 1