天天看點

DZ先生怪談國标28181之利用crontab為linux伺服器做時間同步

1. 自述

關于時間同步,必須一個作為服務端(server)端,一個作為Client端。關于server端,上章DZ先生已經教了WIN7做NTP伺服器的方法:https://blog.csdn.net/dzxs_gb28181/article/details/86992060 今天DZ先生主要講linux作為Client向服務端進行時間同步,此種方法主要面向中小型網絡架構,大型網絡也适用,此方法比較簡單适用。

2. crontab編輯格式介紹

crontab内的基本編輯格式如下

*  *  *  *  *  command

分  時  日  月 周  指令

DZ先生怪談國标28181之利用crontab為linux伺服器做時間同步

周的數字為 0 或 7 時,都代表『星期天』的意思!另外, 還有一些輔助的字元,大概有底下這些:

DZ先生怪談國标28181之利用crontab為linux伺服器做時間同步

幾個基本指令(此操作主要針對root使用者操作)

crontab -e  //編輯某個使用者的cron服務

crontab -l   //列出某個使用者cron服務的詳細内容 

[roo[email protected] ~]# service crond status    #檢視crond狀态   注:定制系統可能沒有這條指令

crond (pid  2775) is running...

[[email protected] ~]# service crond stop       #停止crond服務

Stopping crond:                                            [  OK  ]

[[email protected] ~]# service crond start     #啟動crond服務

Starting crond:                                            [  OK  ]

-------------------------------------------------------------------------------

或者用這種方式也可以

[[email protected] ~]# /etc/init.d/crond status

crond (pid  3639) is running...

[[email protected] ~]# /etc/init.d/crond stop

Stopping crond:                                            [  OK  ]

[[email protected] ~]# /etc/init.d/crond start

Starting crond:                                            [  OK  ]

[[email protected] ~]# /etc/init.d/crond status

crond (pid  3668) is running...

3. 執行個體

現有一台WIN7  NTP伺服器192.168.1.203,linux伺服器每隔一個小時向NTP伺服器進行時間同步

1) crontab -e

0 * * * * /usr/sbin/ntpdate 192.168.1.203>>/var/log/ntpdate.log;/usr/sbin/hwclock -w

0 * * * *   #代表每隔1個小時;如果是每天2點則改為 0 2 * * * 

/usr/sbin/ntpdate 192.168.1.203>>/var/log/ntpdate.log; 

#代表向192.168.1.203進行時間同步,并将日志存入/var/log/ntpdate.log,ntpdate.log 要自己在/var/log下建立

/usr/sbin/hwclock -w; #代表同步系統時間至硬體時間

;  # 一次執行兩個指令用;隔開,或者&&也行

注意事項:

1. 這邊為什麼用/usr/sbin/ntpdate和/usr/sbin/hwclock;為什麼不直接用ntpdate和hwclock?

在crontab中,這邊ntpdate和hwclock不能直接執行,需要找到詳細的指令路徑,可以用whereis指令查詢如:

[[email protected] ~]# whereis ntpdate

ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz

####某些系統做過開發沒有whereis指令,可以試試/usr/local/bin/ntpdate

[[email protected] ~]# whereis hwclock

hwclock: /sbin/hwclock /usr/sbin/hwclock /usr/share/man/man8/hwclock.8.gz

[[email protected] ~]# 

2)crontab -l 檢視例行性工作有哪些

[[email protected] ~]# crontab -l

* * * * * /usr/sbin/ntpdate 192.168.1.203>>/var/log/ntpdate.log&&/usr/sbin/hwclock -w

[[email protected] ~]# 

3)檢視日志

[[email protected] ~]# tail -f /var/log/ntpdate.log 

11 Feb 17:00:01 ntpdate[3155]: adjust time server 192.168.1.203 offset 0.010181 sec  #每隔1小時同步的日志

11 Feb 18:00:01 ntpdate[3190]: adjust time server 192.168.1.203 offset 0.044966 sec  #每隔1小時同步的日志

11 Feb 21:19:01 ntpdate[3460]: adjust time server 192.168.1.203 offset -0.016894 sec #每隔1分鐘同步的日志

11 Feb 21:20:01 ntpdate[3466]: adjust time server 192.168.1.203 offset 0.009187 sec   #每隔1分鐘同步的日志

11 Feb 21:21:01 ntpdate[3472]: adjust time server 192.168.1.203 offset -0.004192 sec  #每隔1分鐘同步的日志

DZ先生個人官方微信

DZ先生怪談國标28181之利用crontab為linux伺服器做時間同步

***關注DZ君,讓監控變得更簡單***

繼續閱讀