How to restart a service if its dependent service is restarted

iobelix picture iobelix · Mar 16, 2016 · Viewed 19.9k times · Source

A service (say bar.service) is dependent on another service (say foo.service), like below

bar's service file:

[Unit]
After=foo.service
Requires=foo.service
...

If foo.service is restarted (either manually or due to a bug), how can bar.service be automatically restarted as well?

Answer

Kevin M Granger picture Kevin M Granger · Mar 16, 2016

You can use PartOf.

[Unit]
After=foo.service
Requires=foo.service
PartOf=foo.service

From the systemd.unit man page:

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.