How to restart a service if its dependent service is restarted

前端 未结 3 1448
生来不讨喜
生来不讨喜 2021-02-05 02:41

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.serv         


        
3条回答
  •  一个人的身影
    2021-02-05 03:30

    A another solution might be to use ExecStartPost option to restart the bar.service (if it´s executed) when foo.service has (re)started:

    # foo.service
    [Service]
    ExecStartPost=/bin/systemctl try-restart bar.service
    Restart=on-failure
    RestartSec=30s
    

    The additional Restart and RestartSec options ensure that foo.service will automatically restarted on crash and thus also bar.service.

    A second extension my be to add same to bar.service and ensure that bar.service start after foo.service:

    # bar.service
    [Unit]
    After=foo.service
    
    [Service]
    Restart=on-failure
    RestartSec=30s
    

    This should start both services automatically in case of a crash and bar.service will be restarted when foo.service restarts (due to an error or manually triggered).

提交回复
热议问题