我有一个依赖于parent.service的service child.service。我希望child.service在parent.service启动或重新启动时启动(或重新启动)。

我已阅读U&L堆栈交换(以及其他堆栈交换站点上的其他相关答案)约一小时,无法让child.service在parent.service重新启动时启动或重新启动。

父服务启动和运行没有问题(例如,它不早退出)。下面是parent.service配置,在/etc/system d/system/parent.service.d/override.conf中:

[Unit]
Description=parent
After=network.target

[Service]
Type=simple
ExecStart=
ExecStart=/usr/bin/parent-start
ExecStartPost=/usr/local/bin/afterparentstart
Restart=always

[Install]
WantedBy=multi-user.target

下面是子服务配置,在/etc/systemd/user/child.service中:

[Unit]
Description=child
Requires=network.target
Wants=nss-lookup.target
Before=nss-lookup.target
After=network.target
Requires=parent.service
PartOf=parent.service
After=parent.service

[Service]
Type=forking
PIDFile=/run/child/child.pid
Restart=always

ExecStart=/usr/local/bin/start-child

# Original file had HUP here but HUP does not trigger closing/reopening the log
# file handle as far as I can tell.
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
WantedBy=parent.service

当我运行"systemctl show parent.service"时,可以看到ConsistsOf=child.service关系存在。但是,如果child.service失败,或者我手动停止它,然后运行 systemctl restart parent.service ,我观察到 systemctl status child.service 子服务尚未启动或尝试启动。

我使用的是Ubuntu18.04和Systemd237版本。我错过了什么?我已经智穷了。谢谢,凯文

最佳回复

啊哈,我在跑 systemctl daemon-reload 但这还不够。我需要重新启用子系统单元,以便创建正确的符号链接集。

$ systemctl reenable /etc/systemd/user/child.service
Created symlink /etc/systemd/system/multi-user.target.wants/child.service → /etc/systemd/user/child.service.
Created symlink /etc/systemd/system/parent.service.wants/child.service → /etc/systemd/user/child.service.
Created symlink /etc/systemd/system/child.service → /etc/systemd/user/child.service.