使用Linux.VPS第二天,发现系统时间不正确,手动修改或者同步正确一次后,运行半天发觉时间又快了几分钟,反正是运行得越久就越快,难道VPS穿越啦?
和VPS管理员沟通无果,再上网搜索一番后,决定使用定时器自动同步时间算啦。
ssh执行命令:
#crontab -e
执行后加入一行:(和vi命令一样,按i进入编辑模式,Esc返回命令模式,:w保存修改,:q退出)
*/30 * * * * /usr/sbin/ntpdate asia.pool.ntp.org >> /var/log/ntp.log 2>&1
数字30表示30分钟执行一次,保存和退出后,还需要重启定时任务器才行哦:
#service crond restart
后面的>> /var/log/crontab.log 表示把定时执行情况写入到日志文件中去,方便查看有无异常。
注意utpdate要写完整路径,网上有些人的例子没写完整路径会导致以下异常:
/bin/sh: ntpdate: command not found
如果你的linux系统根本没有ntpdate这个命令
yum install ntp
#台湾:asia.pool.ntp.org
#中科院:210.72.145.44
#网通:219.158.14.130
ntp.sh
#!/bin/bash
###################
. /etc/profile
. ~/.bash_profile
###################
#微软:time.windows.com
#台湾:asia.pool.ntp.org
#中科院:210.72.145.44
#网通:219.158.14.130
# crontab -e
# 1 * * * * sh /root/ntp.sh >> /var/log/ntp.log 2>&1
ntpdate asia.pool.ntp.org