systemd: Stop dependent service when main service crashes

(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

  • B won't start unless A is running (good).
  • B is stopped when A is stopped (good).
  • However, if I kill A, service B continues to run (bad).

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