如果重新启动其依赖服务,如何重新启动服务
How to restart a service if its dependent service is restarted
一项服务(比如bar.service)依赖于另一项服务(比如foo.service),如下所示
bar的服务文件:
[Unit]
After=foo.service
Requires=foo.service
...
如果 foo.service 重新启动(手动或由于错误),bar.service 如何也自动重新启动?
您可以使用 PartOf
.
[Unit]
After=foo.service
Requires=foo.service
PartOf=foo.service
来自 systemd.unit
手册页:
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.
我认为需要的选项是 BindsTo,它也能处理不当行为。
[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=.
另一种解决方案可能是使用 ExecStartPost 选项在 foo.service 已(重新)启动时重新启动 bar.service(如果已执行) :
# foo.service
[Service]
ExecStartPost=/bin/systemctl try-restart bar.service
Restart=on-failure
RestartSec=30s
额外的 Restart 和 RestartSec 选项确保 foo.service 会在崩溃时自动重启,因此 bar.service.
我的第二个扩展是将相同的内容添加到 bar.service 并确保 bar.service 在 foo.service 之后开始:
# bar.service
[Unit]
After=foo.service
[Service]
Restart=on-failure
RestartSec=30s
这应该会在崩溃的情况下自动启动这两个服务,并且 bar.service 将在 foo.service 重新启动时重新启动(由于错误或手动触发)。
一项服务(比如bar.service)依赖于另一项服务(比如foo.service),如下所示
bar的服务文件:
[Unit]
After=foo.service
Requires=foo.service
...
如果 foo.service 重新启动(手动或由于错误),bar.service 如何也自动重新启动?
您可以使用 PartOf
.
[Unit]
After=foo.service
Requires=foo.service
PartOf=foo.service
来自 systemd.unit
手册页:
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.
我认为需要的选项是 BindsTo,它也能处理不当行为。
[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=.
另一种解决方案可能是使用 ExecStartPost 选项在 foo.service 已(重新)启动时重新启动 bar.service(如果已执行) :
# foo.service
[Service]
ExecStartPost=/bin/systemctl try-restart bar.service
Restart=on-failure
RestartSec=30s
额外的 Restart 和 RestartSec 选项确保 foo.service 会在崩溃时自动重启,因此 bar.service.
我的第二个扩展是将相同的内容添加到 bar.service 并确保 bar.service 在 foo.service 之后开始:
# bar.service
[Unit]
After=foo.service
[Service]
Restart=on-failure
RestartSec=30s
这应该会在崩溃的情况下自动启动这两个服务,并且 bar.service 将在 foo.service 重新启动时重新启动(由于错误或手动触发)。