【问题标题】:Debian systemd service starts before network is ready [closed]Debian systemd 服务在网络准备好之前启动[关闭]
【发布时间】:2015-09-30 18:51:27
【问题描述】:

操作系统:Debian Jessie

我想在每次系统启动时更新用户文件夹中的 git 存储库。

我用 cron @reboot 条目试过这个。 Cron 启动得太早,结果给我发了邮件“SSH:无法解析主机名...”

然后我尝试了 SysV 初始化脚本。效果 - 一样。

目前我正在尝试具有相同错误报告的 systemd 服务。

/usr/bin/git_repo

#! /bin/sh
# Description:       Updates local git repository with latest content
#cd /home/tanglor/repo
su -c'cd repo;git pull' - tanglor

/etc/systemd/system/repo.service

[Unit]
Description = Updates local git repository with latest content
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/repo
[Install]
WantedBy=multi-user.target

还是没有结果。我的意思是在系统日志中我发现:

Sep 30 19:41:59 Khlavan repo[422]: ssh: Could not resolve hostname bitbucket.org: Name or service not known
Sep 30 19:41:59 Khlavan repo[422]: fatal: Could not read from remote repository.
Sep 30 19:41:59 Khlavan repo[422]: Please make sure you have the correct access rights
Sep 30 19:41:59 Khlavan repo[422]: and the repository exists.
Sep 30 19:41:59 Khlavan systemd[1]: repo.service: main process exited, code=exited, status=1/FAILURE
Sep 30 19:41:59 Khlavan systemd[1]: Failed to start Updates local git repository with latest content.
Sep 30 19:41:59 Khlavan systemd[1]: Unit repo.service entered failed state.

如何实现每次系统启动时的仓库更新?

一种解决方案是使用带有 sleep 命令的脚本,它会等到一些 ssh 测试成功然后完成。但这是不得已而为之,我想按部就班地解决这个问题。

【问题讨论】:

  • 这太疯狂了。如果您不经常重启怎么办?
  • One solution is to use script with sleep command which will wait till some ssh test will succeed and then complete. 这是迄今为止我找到的唯一可靠的解决方案。 Systemd 很奇怪。

标签: linux debian systemd


【解决方案1】:

您可能应该查看 systemd 文档以找到最适合您的解决方案,但这里有几个链接可能对您有所帮助。

很多网管方案都提供了无条件拉入network-online.target的方式,从而将network.target的效果升级为network-online.target的效果。

如果您使用 NetworkManager,您可以通过启用 NetworkManager-wait-online.service 来做到这一点:

systemctl enable NetworkManager-wait-online.service

如果您使用 systemd-networkd,您可以通过启用 systemd-networkd-wait-online.service 来做到这一点:

systemctl enable systemd-networkd-wait-online.service

这将确保所有已配置的网络设备都已启动并在继续启动之前分配了 IP 地址。该服务将在 90 年代后超时。即使未达到超时,启用此服务也可能会大大延迟您的启动。默认情况下这两个服务都被禁用。

或者,您可以更改需要网络启动的服务,以包括

After=network-online.target
Wants=network-online.target

(freedesktop.org,2015 年):

检查手册页中的 Before=、After= 选择选项。这些选项可以更改 systemd 启动单元的顺序,然后选择您必须在哪些单元之后启动您的单元。


希望这对您有所帮助,如果不是为了解决您的问题,而是为了找到解决方法。

【讨论】:

  • 非常感谢。启用 NetworkManager-wait-online.service 解决了这个问题。其他方法 - 例如向 network.target 和 network-online.target 添加依赖项是不够的。
  • 我遇到了与 OP 相同的问题,包括 lighttpd、bind 和 postfix(所有绑定到特定 IP)。 network-online.target 似乎不起作用..
猜你喜欢
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2016-06-18
  • 2019-10-22
  • 2016-09-03
  • 1970-01-01
相关资源
最近更新 更多