Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart a service if its dependent service is restarted

Tags:

linux

systemd

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?

like image 575
iobelix Avatar asked Mar 16 '16 18:03

iobelix


People also ask

What is service restart?

The Restart-Service cmdlet sends a stop message and then a start message to the Windows Service Controller for a specified service. If a service was already stopped, it is started without notifying you of an error.

How do you know when a service has stopped in Linux?

Check the service/daemons log files. You may need to review its config file to find where they are located on Linux. Configuration files are typically found in /etc. Logs are commonly found in /var/log on Linux.


1 Answers

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.

like image 144
Kevin M Granger Avatar answered Sep 29 '22 05:09

Kevin M Granger