大网数据广告活动
服务器运维问答

Systemd - start service only after DNS is available

· 52CE 我爱测

I've got a couple services, most notably nginx and ntpd, that depend on having working DNS resolution to start properly. Right now neither of these services start correctly at boot time, but do start fine when manually intervened…

问题原文

原帖链接 · Brando__ · CC BY-SA 3.0

I've got a couple services, most notably nginx and ntpd, that depend on having working DNS resolution to start properly. Right now neither of these services start correctly at boot time, but do start fine when manually intervened on once the machine is up, with some messages in the logs about not being able to resolve names.

This leads me to believe I'm having a race condition with systemd. My servers point to 127.0.0.1 for their nameservers. Binded to localhost:53 is pdns-recursor. I've set ntp and nginx to be WantedBy pdns-recursor in their unit files as follows

[Unit]
WantedBy=pdns-recursor.service

However I still receive log messages in both nginx and ntp about failing to resolve names at boot time.

How can I verify that DNS is completely up before these services attempt to start? I am using Ubuntu 16.04

Aug 09 22:35:25 host.blah ntpd[3574]: restrict: ignoring line 21, address/host 'ntp.blah' unusable.
Aug 09 22:35:26 host.blah ntpd[3574]: restrict: ignoring line 23, address/host 'ntp.blah' unusable.
Aug 09 22:35:28 host.blah ntpd[3574]: restrict: ignoring line 25, address/host 'ntp.blah' unusable.
Aug 09 22:35:29 host.blah ntpd[3574]: restrict: ignoring line 27, address/host 'ntp.blah' unusable.

社区高分解答

原帖链接 · mianos · CC BY-SA 4.0

systemd is flexible enough to do this:

[Unit]
Description=Wait for DNS to come up using 'host'
After=nss-lookup.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'until host yourhost.com; do sleep 1; done'

[Install]
WantedBy=multi-user.target

Then you can depend on this unit in your unit file that requires dns to mount:

[Unit]
Description=mount nfs
After=dns-ready.service

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。