Debian systemd 服务在网络准备就绪之前启动
Debian systemd service starts before network is ready
OS:Debian 杰西
我想在每次系统启动时更新 git 用户文件夹中的存储库。
我用 cron @reboot 条目试过了。 Cron 启动得太早,结果给我发了 "SSH: Could not resolve hostname ..."
然后我尝试了 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.服务
[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 测试成功然后完成。但这是不得已的办法,我想按部就班地解决这个问题。
您可能应该查看 systemd 文档以找到最适合您的解决方案,但这里有一些链接可能会对您有所帮助。
- Archlinux forum @Lone_Wolf
- Unix & Linux (probably duplicate)
- Freedesktop - Running Services After the Network is up
很多网管方案都提供了无条件拉入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= selection 选项。这些是更改 systemd 启动单元的顺序的选项,然后 select 之后你必须启动你的单元。
希望这对您有所帮助,如果不是解决您的问题,而是找到解决问题的方法。
OS:Debian 杰西
我想在每次系统启动时更新 git 用户文件夹中的存储库。
我用 cron @reboot 条目试过了。 Cron 启动得太早,结果给我发了 "SSH: Could not resolve hostname ..."
然后我尝试了 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.服务
[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 测试成功然后完成。但这是不得已的办法,我想按部就班地解决这个问题。
您可能应该查看 systemd 文档以找到最适合您的解决方案,但这里有一些链接可能会对您有所帮助。
- Archlinux forum @Lone_Wolf
- Unix & Linux (probably duplicate)
- Freedesktop - Running Services After the Network is up
很多网管方案都提供了无条件拉入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= selection 选项。这些是更改 systemd 启动单元的顺序的选项,然后 select 之后你必须启动你的单元。
希望这对您有所帮助,如果不是解决您的问题,而是找到解决问题的方法。