延迟进一步启动,直到时间同步

延迟进一步启动,直到时间同步

我正在运行带有 Raspbian Stretch (Debian) 的 Raspberry Pi Model 2B。

我有一个 UI 应用程序(chromium-browser,通过 启动/home/pi/.config/lxsession/LXDE-pi/autostart)和一个通过 cron 启动的脚本,这两者都取决于它们启动时设置的实际当前时间(或者我必须在代码中添加一些丑陋的 hacks处理它)。

启动时间滞后几个小时,直到某个时刻时间通过 WLAN 同步。

我在想也许我可以以某种方式延迟自动启动和 cron 的执行,直到 NTP 有机会同步。

我该怎么做呢?或者我可以通过其他方式开始两者吗?

答案1

Raspberry Pi SE 网站上的问答题对此进行了介绍:如何强制 NTPD 在启动后更新日期/时间?

方法#1

这种方法看起来是较新的首选方法:

$ timedatectl
      Local time: Thu 2018-07-26 16:10:31 EDT
  Universal time: Thu 2018-07-26 20:10:31 UTC
        RTC time: n/a
       Time zone: America/New_York (EDT, -0400)
 Network time on: no
NTP synchronized: yes
 RTC in local TZ: no

如果未启用:

$ timedatectl set-ntp True
root@pi-hole:~ # timedatectl
      Local time: Thu 2018-07-26 16:11:45 EDT
  Universal time: Thu 2018-07-26 20:11:45 UTC
        RTC time: n/a
       Time zone: America/New_York (EDT, -0400)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

方法#2

这种方法看起来是执行此操作的较旧方法:

$ sudo apt-get install ntpdate

根据这个问题:

ntpdate将在以太网接口启动时运行,并从 ntp 服务器设置时间(请参阅 参考资料/etc/default/ntpdate)。

如果ntpd正在运行,ntpdate则不会执行任何操作,但ntpdate会在启动之前运行ntpd- 因此只要有以太网连接,就应该设置启动时间。

顺便说一句,我的 Raspian 版本也没有启用此功能:

$ more /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

我对方法 1 和 2 的结论

进一步研究这些解决方案,我没有看到任何迹象表明它实际上会在哪里运行,因此其他非盟问答的评论对我来说有点令人困惑。如果它连接到 NIC 的 up 状态,我希望看到如下内容:

$ cat /etc/network/interfaces
auto eth0
    iface eth0 inet dhcp
    post-up /usr/local/sbin/update-time.sh

# REF: https://askubuntu.com/a/708832/17531

方法#3

鉴于上述情况,我最终也在我的 Raspberry Pi (Raspian) 上实现了这样的 NTP 同步。

最后我只是将其添加到我的/etc/rc.local

$ cat /etc/rc.local
logger "Beginning force syncing NTP..."
service ntp stop
ntpd -gq
service ntp start
logger "Finished force syncing NTP..."

结果是/var/log/syslog

Aug  5 09:10:20 pi-hole ntp[526]: Starting NTP server: ntpd.
Aug  5 09:10:20 pi-hole ntpd[555]: proto: precision = 1.458 usec (-19)
Aug  5 09:10:20 pi-hole ntpd[555]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:20 pi-hole ntpd[555]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:21 pi-hole ntpd[555]: ntpd exiting on signal 15 (Terminated)
Aug  5 09:10:21 pi-hole ntp[661]: Stopping NTP server: ntpd.
Aug  5 09:10:21 pi-hole ntpd[674]: ntpd [email protected] Sat Mar 10 18:03:33 UTC 2018 (1): Starting
Aug  5 09:10:21 pi-hole ntpd[674]: Command line: ntpd -gq
Aug  5 09:10:21 pi-hole ntpd[674]: proto: precision = 0.625 usec (-21)
Aug  5 09:10:21 pi-hole ntpd[674]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:21 pi-hole ntpd[674]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:34 pi-hole ntpd[674]: Soliciting pool server 198.98.57.16
Aug  5 09:10:35 pi-hole ntpd[674]: Soliciting pool server 74.82.59.149
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 23.131.160.7
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 52.37.26.163
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 204.2.134.163
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 96.126.100.203
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 199.223.248.101
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 66.228.42.59
Aug  5 09:10:36 pi-hole ntpd[674]: Soliciting pool server 208.75.89.4
Aug  5 09:10:37 pi-hole ntpd[674]: Soliciting pool server 204.2.134.164
Aug  5 09:10:37 pi-hole ntpd[674]: Soliciting pool server 104.236.116.147
Aug  5 09:10:38 pi-hole ntpd[674]: Soliciting pool server 204.9.54.119
Aug  5 09:10:48 pi-hole ntpd[674]: ntpd: time set +6.769186 s
Aug  5 09:10:49 pi-hole ntpd[853]: ntpd [email protected] Sat Mar 10 18:03:33 UTC 2018 (1): Starting
Aug  5 09:10:49 pi-hole ntpd[853]: Command line: /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:114
Aug  5 09:10:49 pi-hole ntp[844]: Starting NTP server: ntpd.
Aug  5 09:10:49 pi-hole ntpd[856]: proto: precision = 0.625 usec (-21)
Aug  5 09:10:49 pi-hole ntpd[856]: Listen and drop on 0 v6wildcard [::]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 2 lo 127.0.0.1:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 3 eth0 192.168.1.85:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 4 lo [::1]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listen normally on 5 eth0 [fe80::f5ea:7663:4ec:784d%2]:123
Aug  5 09:10:49 pi-hole ntpd[856]: Listening on routing socket on fd #22 for interface updates
Aug  5 09:10:49 pi-hole root: Finished force syncing NTP...

为什么我需要 rc.local?

我在调查这个问题时注意到的事情围绕着其他 NTP 服务的安装,例如chronyntpd。将这些放在系统上会否定 systemd 的 timedatectl 功能,主要是通过以下文件:

$ cat /lib/systemd/system/systemd-timesyncd.service.d/disable-with-time-daemon.conf
[Unit]
# don't run timesyncd if we have another NTP daemon installed
ConditionFileIsExecutable=!/usr/sbin/ntpd
ConditionFileIsExecutable=!/usr/sbin/openntpd
ConditionFileIsExecutable=!/usr/sbin/chronyd
ConditionFileIsExecutable=!/usr/sbin/VBoxService

结果是这样的:

$ systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: inactive (dead)
Condition: start condition failed at Sun 2018-08-05 10:05:53 EDT; 1s ago
           └─ ConditionFileIsExecutable=!/usr/sbin/ntpd was not met
     Docs: man:systemd-timesyncd.service(8)

删除所有内容,以便我们只依赖 的systemdtimesyncd 设施:

$ cat /lib/systemd/system/systemd-timesyncd.service.d/disable-with-time-daemon.conf |awk -F'=!' '/Condition/ {print $2}' | xargs ls
ls: cannot access '/usr/sbin/ntpd': No such file or directory
ls: cannot access '/usr/sbin/openntpd': No such file or directory
ls: cannot access '/usr/sbin/chronyd': No such file or directory
ls: cannot access '/usr/sbin/VBoxService': No such file or directory

现在允许它运行:

$ systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Sun 2018-08-05 10:14:44 EDT; 5s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 3714 (systemd-timesyn)
   Status: "Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org)."
   CGroup: /system.slice/systemd-timesyncd.service
           └─3714 /lib/systemd/systemd-timesyncd

Aug 05 10:14:44 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:14:44 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:14:45 pi-hole systemd-timesyncd[3714]: Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org).

在 Raspian 上(假设 Ubuntu 和 Debian 也是这样),配置文件中的注释似乎timesyncd.conf表明已经有 NTP 服务器硬编码在:

$ cat /etc/systemd/timesyncd.conf
...
...
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

强制同步

似乎没有任何方法可以直接强制同步。重新启动systemd-timesyncd服务实际上会触发重新同步:

$ systemctl restart systemd-timesyncd

您可以在日志中看到这一点:

$ journalctl -u systemd-timesyncd
-- Logs begin at Thu 2016-11-03 13:16:42 EDT, end at Sun 2018-08-05 10:44:37 EDT. --
Aug 05 10:14:44 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:14:44 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:14:45 pi-hole systemd-timesyncd[3714]: Synchronized to time server 108.59.2.24:123 (0.debian.pool.ntp.org).
Aug 05 10:44:37 pi-hole systemd[1]: Stopping Network Time Synchronization...
Aug 05 10:44:37 pi-hole systemd[1]: Stopped Network Time Synchronization.
Aug 05 10:44:37 pi-hole systemd[1]: Starting Network Time Synchronization...
Aug 05 10:44:37 pi-hole systemd[1]: Started Network Time Synchronization.
Aug 05 10:44:37 pi-hole systemd-timesyncd[4157]: Synchronized to time server 208.75.88.4:123 (0.debian.pool.ntp.org).

最后一行来自我的重启。除了重启之外,systemd-timesyncd此特定版本的 Raspian 上的版本不包含此补丁 -timesync:使轮询间隔可配置#7268,因此除了重新启动之外您无法控制同步。

在更高版本中,systemd-timesyncd您可以使用此选项:

   PollIntervalMinSec=, PollIntervalMaxSec=

   The minimum and maximum poll intervals for NTP messages. Each setting takes a time 
   value (in seconds). PollIntervalMinSec= must not be smaller than 16 seconds. 
   PollIntervalMaxSec= must be larger than PollIntervalMinSec=. PollIntervalMinSec= 
   defaults to 32 seconds, and PollIntervalMaxSec= defaults to 2048 seconds.

参考

答案2

所以我最终要做的是在脚本和应用程序中实现一些基本的时间跳跃识别。

相关内容