尽管配置中有 After=、Requires= 和 PartOf=,但依赖的系统服务未启动

尽管配置中有 After=、Requires= 和 PartOf=,但依赖的系统服务未启动

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

我已经阅读 U&L Stack Exchange(以及其他 Stack Exchange 站点上的其他相关答案)大约一个小时了,当 Parent.service 重新启动时,我无法让 child.service 启动或重新启动。

父服务启动并运行时没有任何问题(例如,它不会提前退出)。这是parent.service配置,位于/etc/systemd/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 showparent.service”时,我可以看到存在 ConsistsOf=child.service 关系。但是,如果 child.service 失败,或者我手动停止它,然后运行systemctl restart parent.service,我会观察到systemctl status child.service子服务尚未启动或尝试启动。

我使用的是 Ubuntu 18.04 和 systemd 版本 237。我在这里缺少什么?我已经无计可施了。谢谢,凯文

答案1

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

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

相关内容