crontab中的ntpdate 时间同步不生效

Shell命令
1972
linux
linux 举报
2020-11-04

每10分钟与服务器同步

crontab -e
0-59/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP

加入定时计划任务,每隔10分钟同步一下时钟。

发现crontab是有自己的环境变量的 path=/usr/bin/;/bin/

但是用命令 whereis ntpdate 发现他在 /usr/sbin 下,所以就有找不到这个命令的可能。然后不全命令的全路径,问题解决。


ntpdate有个致命的问题是他的时间同步存在时间的跳跃,比如现在是12点整,同步完后是1点整,这样对一些依赖时间的操作就会出现问题,比如sleep,wait等。所以很多人推荐只在机器重启后一次性的执行ntpdate,之后用ntpd来同步。


在crontab中添加了 时间同步任务 

10 */1 * * * ntpdate -u 10.11.0.35;hwclock -w

但是时间还是一直不正确

 

开始检查

less /var/log/cron

May 25 12:25:01 localhost CROND[29708]: (root) CMD (ntpdate -u 10.11.0.35;hwclock -w)

命令确实按时执行了

再来看下执行结果

# less /var/spool/mail/root

/bin/sh: ntpdate: command not found
/bin/sh: hwclock: command not found

问题出在这里 ,在/bin/sh  下 找不到  这两个命令

 

解决:看看这两个命令在哪里

# which ntpdate
/usr/sbin/ntpdate

# which hwclock 
/usr/sbin/hwclock

 

重新更改crontab定时任务

#crontab -e
25 */1 * * * /usr/sbin/ntpdate -u 10.11.0.35;/usr/sbin/hwclock -w

#service crond restart

完成


倒序看帖 只看楼主
热点关注
/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
服务停止、启动、重启一体化脚本,万能语言启动脚本
shell参数判断
在后台用进程名运行bash脚本
shell脚本每行后面多了一个^M的原因和解决办法
定时检测服务,检测down掉后重启。系统检测到DMSERVER运行正常
挂载磁盘的时候遇到一个问题,挂载完磁盘之后重启服务器挂载的盘丢失了.
中标麒麟系统Your trial is EXPIRED and no VALID licens
termux 开启ssh并用用户名和密码登陆
组成共享库的目标文件和一般的目标文件有所不同,在编译时要加-fPIC和-share选项,例如:
gcc指定链接库路径后还是找不到库文件
./configure -build,-host,-target设置
shell脚本每行后面多了一个^M的原因和解决办法
shell 中 if [ “x${var}“ == “x“ ] 中 x的作用
查看linux机器是32位还是64位的方法