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?

share|improve this question
    
Have you tried to put the line 'Restart=always' in your service ? I guess bar should be restarted as soon as its dependency is alive – ogs Mar 16 '16 at 22:08
1  
@SnP Restart "Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached" and thus would not help unless bar crashes when foo restarts. – Kevin M Granger Mar 16 '16 at 22:12
    
Quite interesting ! Thank for the clarification – ogs Mar 16 '16 at 22:16
up vote 14 down vote accepted

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.

share|improve this answer
    
Note that PartOf only links restarting, but not stop, upgrade, start events. In those cases you may need WantedBy. See this answer for details – Rennex Jul 21 '17 at 13:10

I think the needed option is BindsTo, it handles the misbehaviours too.

[Unit]
Requires=postgresql.service
After=postgresql.service
BindsTo=postgresql.service

BindsTo=

Configures requirement dependencies, very similar in style to Requires=. However, this dependency type is stronger: in addition to the effect of Requires= it declares that if the unit bound to is stopped, this unit will be stopped too. This means a unit bound to another unit that suddenly enters inactive state will be stopped too. Units can suddenly, unexpectedly enter inactive state for different reasons: the main process of a service unit might terminate on its own choice, the backing device of a device unit might be unplugged or the mount point of a mount unit might be unmounted without involvement of the system and service manager.

When used in conjunction with After= on the same unit the behaviour of BindsTo= is even stronger. In this case, the unit bound to strictly has to be in active state for this unit to also be in active state. This not only means a unit bound to another unit that suddenly enters inactive state, but also one that is bound to another unit that gets skipped due to a failed condition check (such as ConditionPathExists=, ConditionPathIsSymbolicLink=, … — see below) will be stopped, should it be running. Hence, in many cases it is best to combine BindsTo= with After=.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.