无法让ntpdate cron运行

我正试图使用每日计划任务更新我的12.04服务器的时钟。我已经按照这里的说明进行了操作,但它仍然不能正常运行:几周后,时钟再次慢了几秒。我一直在谷歌上搜索解决方案,但似乎没有什么帮助。根据我在谷歌上找到的信息,我已验证了以下内容:

手动运行脚本正常:

root@...:~# /etc/cron.daily/ntpdate
13 May 14:42:22 ntpdate[6927]: adjust time server 96.44.142.5 offset -0.018984 sec

脚本是可执行的。
root@...:~# ls -l /etc/cron.daily/
total 60
....
-rwxr-xr-x 1 root root    46 May 12 20:41 ntpdate
....

我在系统日志中没有看到任何与cron相关的错误。
May 13 06:25:01 ... CRON[6704]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))

我使用了绝对路径,以防万一cron的路径丢失:
/usr/sbin/ntpdate ntp.ubuntu.com pool.ntp.org

我已经做了一次试运行。
root@...:~# run-parts --test /etc/cron.daily
...
/etc/cron.daily/ntpdate
...

我看不出任何原因导致它不能正常工作。

你会在控制台运行命令时返回输出吗? - L. D. James
好的,已经编辑了我的问题以包含那个。 - Theron Luhn
2个回答

确保你有一个 shebang "#!/bin/bash" 来指定脚本的解析方式。 ntpdate 脚本:
#!/bin/bash
/usr/sbin/ntpdate ntp.ubuntu.com pool.ntp.org

作为使用cron的替代方案,您可以安装ntp,让守护程序在服务器运行时自动调整时钟。

是的,这比运行cron要好。而且非常简单:https://help.ubuntu.com/lts/serverguide/NTP.html - steve
但在某些情况下,它并不起作用:例如在虚拟机中,即使守护进程ntp/ntpd正在运行,系统时钟在几周后可能会与正确时间相差几分钟。在这种情况下,需要停止ntpd,运行ntpdate,然后再运行ntpd。 - Jack