[转自

qing_gee的专栏 :http://blog.csdn.net/qing_gee/article/details/42234997

]

前言:在我们的项目中,需要同步Linux服务器的时间,于是用到了ntpdate命令,就在crontab中进行了配置,设置为每个小时的整时分钟进行同步,完了后就一直没有关注,因为这很简单,经验主义告诉我,既然手动使用ntpdate命令就可以同步时间成功,放在cron进程(是Linux的一个守护进程,如果启动后,会每隔一分钟去检查对应的crontab文件,root用户的在/var/spool/cron)中就更没有问题了,然而,今天看Linux服务器的时间后,发现和Windows时间差上了好几分钟,我们的Linux服务承载着期货交易平台的定时服务,比如说,9点半开盘等等,这就意味着时间不准确啊,于是就调查解决问题,有了下面的记录

1.使用crontab -l命令查看定时服务
[root@MyCloudServer xxx]# crontab -l
0,10,20,30,40,50 * * * * ntpdate time.windows.com &>/xxx/ntpdate.log
2.看起来好像没有问题啊,vim /var/spool/mail/root(定时服务日志会存放在该文件中)查看定时服务日志,发现有如下信息
/bin/sh: ntpdate: command not found

说明定时服务在/bin/sh目录中去找ntpdate命令,并且没有找到

3.使用whereis ntpdate命令看看该命令在什么目录下
[root@MyCloudServer cron]# whereis ntpdate
ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz

问题找到了,在定时服务中,ntpdate命令要使用全路径

4.使用crontab -e命令修改一下,加上ntpdate命令的目录
0,10,20,30,40,50 * * * * /usr/sbin/ntpdate time.windows.com &>/xxx/ntpdate.log

然而保存后,等到整数分钟后,在日志中没有发现该命令执行,为什么呢,猜想如下

a.以上命令格式错误,时间格式错误

b.cron自动服务没有执行

通过网上查找时间的命令格式,发现

0,10,20,30,40,50 * * * *

并没有错误,而手动执行

/usr/sbin/ntpdate time.windows.com &>/xxx/ntpdate.log

也成功执行,那么就看看b是否存在问题

5.执行ps -ef | grep cron,查看时间服务进程是否存在
[root@MyCloudServer xxx]# ps -ef | grep cron
root 26157 22992 0 10:04 pts/3 00:00:00 grep cron

发现没有cron执行进程

6.执行service crond status查看服务状态
[root@MyCloudServer xxx]# service crond status
crond is stopped

竟然服务没有启动,好吧

7.启动进程,并且查看状态
[root@MyCloudServer xxx]# service crond start
Starting crond: [ OK ]
[root@MyCloudServer xxx]# service crond status
crond (pid 26291) is running...
[root@MyCloudServer xxx]# ps -ef | grep cron
root 26291 1 0 10:06 ? 00:00:00 crond
root 26302 22992 0 10:06 pts/3 00:00:00 grep cron
8.服务启动了,通过vim /etc/rc.d/rc.local命令添加以下语句设置为开机启动
/sbin/service crond start

注意也加上了/sbin目录

9.最后再看看ntpdate.log中有没有执行日志
[root@MyCloudServer xxx]# cat ntpdate.log
29 Dec 11:10:16 ntpdate[29960]: no server suitable for synchronization found

发现服务器没有找到对应的服务同步,那么猜想应该是time.windows.com服务器在本台服务器上没有获取成功,由于我们用的是香港的云服务器,那么换一个香港认可的地址试试

0,10,20,30,40,50 * * * * /usr/sbin/ntpdate stdtime.gov.hk &>/xxx/ntpdate.log

然后等到整时分钟的时候再次查看一下

[root@MyCloudServer xxx]# cat ntpdate.log
29 Dec 11:20:01 ntpdate[30580]: adjust time server 118.143.17.82 offset 0.015206 sec

可以看到执行成功了

总结:通过以上问题调查,发现无论什么时候经验主义并不可靠,小小的一个问题都可能引发很多原因。

linux 时间同步的更多相关文章

  1. Linux时间同步方式记录

    Linux时间同步 部署Hadoop集群,遇到各个linux服务器的时间不同步的问题,于是研究了一下linux的时间同步方式,特将同步方式,总结如下: A. 前提条件 a)         网络是连通 ...

  2. linux时间同步,ntpd、ntpdate

    linux时间同步,ntpd.ntpdate 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系 ...

  3. linux 时间同步的2种方法(转)

    linux 时间同步的2种方法 张映 发表于 2012-10-23 分类目录: 服务器相关 标签:linux, ntp, 同步, 时间服务器 由于硬件的原因,机器或多或少的根标准时间对不上,一个月的误 ...

  4. Linux 时间同步 01 简介

    Linux 时间同步 01 简介 目录 Linux 时间同步 01 简介 时间同步 公共NTP服务器地址及IP 系统时间相关文件 时间同步 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同 ...

  5. Linux 时间同步 03 ntpdate时间同步

    Linux 时间同步 03 ntpdate时间同步 目录 Linux 时间同步 03 ntpdate时间同步 安装ntpdate 修改/etc/sysconfig/ntpdate 使用ntpdate手 ...

  6. Linux 时间同步 04 ntp时间同步

    Linux 时间同步 04 ntp时间同步 目录 Linux 时间同步 04 ntp时间同步 安装ntp 配置与外部时间服务器进行时间同步的客户端主机 配置其他客户端与以上客户端主机时间同步 验证查看 ...

  7. Linux 时间同步 02 ntpd、ntpdate的区别

    Linux 时间同步 02 ntpd.ntpdate的区别 目录 Linux 时间同步 02 ntpd.ntpdate的区别 [一]这样做不安全. [二]这样做不精确. [三]这样做不够优雅. ntp ...

  8. Linux 时间同步 05 chrony时间同步

    Linux 时间同步 05 chrony时间同步 目录 Linux 时间同步 05 chrony时间同步 chrony 的优势: chrony包介绍 安装chrony 配置与外部时间服务器进行时间同步 ...

  9. linux 时间同步的2种方法

    转载自: http://blog.51yip.com/server/1474.html 由于硬件的原因,机器或多或少的根标准时间对不上,一个月的误差几秒到几分钟不等.对于服务器来说时间不准,会有很多麻 ...

  10. Linux时间同步,ntpdate命令、ntpd服务详解

    声明:以下内容来自网友整理(http://blog.sina.com.cn/s/blog_636a55070101u1mg.html),为便于以后学习暂时收录,请不要随意转载 Linux默认情况下,系 ...

随机推荐

  1. RY哥查字典

    时间限制: 1 s 空间限制: 16000 KB 题目等级 : 钻石 Diamond 题目描述 Description RY哥最近新买了一本字典,他十分高兴,因为这上面的单词都十分的和谐,他天天查字典 ...

  2. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  3. documentfragment

    JS临时容器,父类是null,存储实际是把存储对象所有子类存储在里面,localStorage,浏览器支持情况下,保存本地变量

  4. EXCEL导入(反射)

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  5. Linux破解root密码

    实验环境                 虚拟机软件:VMware Workstation                 操作系统:Read Hat Enteprise 6.3      1.破解r ...

  6. Unity全视角游戏的键盘操作位移——研究笔记

    using UnityEngine; using System.Collections; public class MoveCeShi : MonoBehaviour { ; private Char ...

  7. JSPatch – 动态更新iOS APP

    原文:http://blog.cnbang.net/works/2767/ JSPatch是最近业余做的项目,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的 ...

  8. Java BigDecimal 加减乘除运算

    加法:add 减法:subtract 乘法:multiply 除法:divide BigDecimal bignum1 = new BigDecimal("10"); BigDec ...

  9. .NET的Cookie相关操作

    using System; using System.Collections.Generic; using System.Text; using System.Web; namespace Comm ...

  10. 解决“动软代码生成器在SqlServer中会将唯一索引识别为主键"的Bug

    动软代码生成器在SqlServer中,生成的代码会将唯一索引错误地识别为主键, 反编译源代码后,发现其中的SQL条件有误,现修复此Bug. 修复方法:将附件中的”Maticsoft.DbObjects ...