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

Synchronize clock with NTP while online, and with RTC while offline?

· 52CE 我爱测

Is there a existing mechanism that synchronizes a linux system with NTP while online, and with a predictably drifting RTC while offline? We operate remote "collectors": embedded Linux systems that collect and timestamp sensor dat…

问题原文

原帖链接 · Nils Toedtmann · CC BY-SA 4.0

Is there a existing mechanism that synchronizes a linux system with NTP while online, and with a predictably drifting RTC while offline?


We operate remote "collectors": embedded Linux systems that collect and timestamp sensor data. We need their clock errors to stay reasonably small, say below 5 seconds. Usually we use NTP to sync their clocks, and that works fine - as long as the system is online.

The problem is that some collectors have very bad uplinks which can go down for hours, days or even weeks. That doesn't stop the local data collection, but without NTP, the Linux system clock drifts badly and quite unpredictably.

OTOH, the hardware's RTC drifts heavily too, but at a constant rate. The RTC drift rate varies from board to board, but is constant per board and can be measured.

I guess what we need is a mechanism that does the following:

  • Measure the RTC drift rate of a board before its deployment
  • Adjust system time ongoing/regularly via NTP when possible
  • Adjust system time regularly from RTC when NTP is unavaiable. Take known RTC drift rate into account.
  • Optional: Measure and record the RTC drift rate ongoing while being online (1)

With 'mechanism' i mean some well-maintained, documented piece of software and/or config that can handle the two states "online" vs. "offline", ensure that the system clock is synchronized with the correct time source (ntp vs. rtc), detect change of state, and correct for the RTC drift. It doesn't matter much whether it is implemented as a special ntpd configuration/plugin, as a separate daemon, as a cron job, or else.

I had a look at Chrony, but according to its documentation it tries to predict the drift of the system clock, which in our case drifts far more unpredictably than the RTC. Chrony seems to use the RTC only to keep time across reboots.


(1) Note ntpd activates the kernel's '11-minute mode' (update rtc from system clock every 11 minutes). There seems to be no ways with current kernels and ntpd to prevent the 11-minute mode. Therefore, any rtc drift information gets lost while ntpd is running (thx @billthor).


Updates/edits:

  • We are considering to add an external radio clock for the MSF or DCF77 signal (we are based in Europe) via USB or Serial. But we rather keep the hardware lean.
  • Our collectors are located indoors, often in the basement. So adding GPS clocks won't help.
  • We use Debian 7. That means hwclock from util-linux-2.20.1, ntpdate-4.2.6p5, ntpd from ntp-4.2.6.p5, chrony-1.24 (potentially 1.30).
  • Note that our problem is not that we don't know how to use ntpdate(8), hwclock(8), date(1), etc. Please see the added section in italics about what i mean with 'mechanism'.
  • Added footnote about the '11-minute mode'
  • Here is a very interesting discussion about offline-sync and RTC drift

社区高分解答

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

Your situation is unusual, and I'd be surprised if anyone comes up with a standard ntpd-based configuration to do what you want. That said, I like being surprised, and it happens quite often around these parts.

But until someone comes up with a better idea, have you considered a crontab entry like this?

*/5 * * * *   ntpdate 0.pool.ntp.org || ( hwclock --adjust; hwclock --hctosys )

IE, every five minutes try to sync the clock via ntpdate, and if (and only if) that fails, adjust the hardware clock for drift according to the /etc/adjtime file (whose format is detailed in man hwclock, and whose first line you have populated appropriately using your knowledge of that particular RTC's rate), then set the system clock from the RTC.

Note that if you go for a solution like this, and you are deploying any significant number of these systems, it is considered polite to work with the pool, and contribute servers back in proportion to your usage. You can find more information at http://www.pool.ntp.org/en/vendors.html .

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

资料来源与适用范围

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

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

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