Linux系统同步时间脚本

  Linux操作系统,如果时间和网络时间差距太大的话。可能会导致程序,进程启动不了。所以linux系统时间同步显得尤为重要,本文在借鉴网上众多资料后,以centos_6.X系统为例,整理同步时间的脚步,如有不合理的地方,敬请说明。

一、创建脚本目录

[root@shell ~]# mkdir /usr/local/scripts

二、脚本内容

[root@shell ~]# cat /usr/local/scripts/Sync_NTP.sh
cat /root/ntp.sh 

#!/bin/bash

#######################################################
# Function: Inspection Time
# Author: davie
# Date: 2017-12-02
# Version: 1.0
# QQ: 178570692
# Script name: /usr/local/scripts/Sync_NTP.sh
# http://www.cnblogs.com/bjx2020/
#######################################################
# List of NTP's servers ntp_cmd="/usr/sbin/ntpdate"
if [ ! -f "${ntp_cmd}" ]; then
echo "/usr/sbin/ntpdate does not exists!"
yum -y install ntp* >/dev/null
fi #NTP服务器数组列表
ntpServer=(
[0]=0.cn.pool.ntp.org
[1]=1.cn.pool.ntp.org
[2]=2.cn.pool.ntp.org
[3]=3.cn.pool.ntp.org
)
#校验#
serverNum=`echo ${#ntpServer[*]}`
NUM=0
for ((i=0; i<=$serverNum; i++)); do
echo -n "正在和NTP服务器:${ntpServer[$NUM]}校验中..."
/usr/sbin/ntpdate ${ntpServer[$NUM]} >> /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "\e[1;32m\t[成功]\e[0m"
echo -e "\e[1;32m同步成功,退出......\e[0m"
break
else
echo -e "\e[1;31m\t[失败]\e[0m"
echo -e "\e[1;31m继续同步下一个!!!!!\e[0m"
let NUM++
fi
sleep 2
done
# chmod +x /root/ntp.sh

# sh /root/ntp.sh

三、执行脚本测试

[root@shell ~]# mkdir /usr/local/scripts
[root@shell ~]# vim /usr/local/scripts/Sync_NTP.sh
[root@shell ~]# chmod +x /usr/local/scripts/Sync_NTP.sh
[root@shell ~]# sh /usr/local/scripts/Sync_NTP.sh
Sync time with the NTP server: .cn.pool.ntp.org Checking... [ success ]
[root@shell ~]#

演示一个不能同步某个服务器时间的例子。故意修改第一个NTP服务器的网址即可。

这里将:[0]=1.cn.pool.ntp.org

修改为: [0]=xx1.cn.pool.ntp.org

再次执行脚本:

[root@shell ~]# sh /usr/local/scripts/Sync_NTP.sh
Sync time with the NTP server: xx1.cn.pool.ntp.org Checking... [ fail ]
Continue to sync the next!!!
Sync time with the NTP server: .cn.pool.ntp.org Checking... [ success ]
[root@shell ~]#

四、将其加入计划任务中

每隔一小时同步一次。前期时间同步一致后,建议将其间隔调整大一点。

[root@shell ~]# crontab -l
*/ * * * /usr/local/scripts/Sync_NTP.sh >/dev/null
[root@shell ~]#

五、说明

有些系统在安装时,没有ntp相关命令,就需要用yum安装。

注意:系统本身需要同步外部服务器时间,所以需要连接外网。对于内网服务器有安全要求限制的话,只需要将ntp服务器网址修改成对应的内网时间服务器即可。

[root@shell ~]# /usr/sbin/ntpdate .cn.pool.ntp.org
-bash: /usr/sbin/ntpdate: No such file or directory
[root@shell ~]# yum -y install ntpd*

Shell 同步时间脚本的更多相关文章

  1. linux 下使rdate命令支持ipv6 ntp server 同步时间

    如果使用linux 下,busybox自带的rdate命令 去ipv6 的ntp server 同步时间的话,会提示invalid argument :无效参数. 那么现在下载rdate的源码并对其进 ...

  2. 03: shell简单监控脚本

    1.1 监控apache web server #! /bin/bash # apache netstat -anpt | grep 80 &> /dev/null if [ $? -e ...

  3. Shell编程-01-Shell脚本初步入门

    目录 什么是Shell 什么是Shell脚本 Shell脚本语言的种类 常用操作系统默认Shell Shell 脚本的建立和执行 脚本规范 什么是Shell     简单来说Shell其实就是一个命令 ...

  4. 生产力工具:shell 与 Bash 脚本

    生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...

  5. Shell日期时间和时间戳的转换

    Gitlab的备份文件是以时间戳显示的,类似:1438624820_gitlab_backup.tar 为了更易于阅读,想把文件名转换成日期格式:2015-08-04_gitlab_backup.ta ...

  6. python第三方库推荐 - 通过ntplib在windows上同步时间

    很多时候我们有通过程序脚本同步校正北京时间的需求. 在linux上同步时间比较方便,安装个ntpdate软件就行了. 但是在windows的要同步时间比较麻烦. 这时想到的就是从网络获取一个准确的时间 ...

  7. shell获取时间的相关命令

    Linux shell获取时间和时间间隔(ms级别) 说明:在进行一些性能测试的时候,有时候我们希望能计算一个程序运行的时间,有时候可能会自己写一个shell脚本方便进行一些性能测试的控制(比如希望能 ...

  8. Raspberry Pi开发之旅-同步时间

    使用htpdate同步时间 由于树莓派板子上没有 RTC 硬件和电池,因此树莓派上的系统时间重启是保存不了的.网上已经有人想到应对 NTP 被防火墙封掉类似的需求了,开源的 htpdate 命令直接使 ...

  9. Linux离线同步时间

    Linux离线同步时间 思路:以其中一台时间为准 脚本 #!/bin/shcurrent=`date '+%H:%M:%S'` for i in bigdata1 bigdata2 bigdata3 ...

随机推荐

  1. 用“人话”解释不精确线搜索中的Armijo-Goldstein准则及Wolfe-Powell准则

    转载请注明出处:http://www.codelast.com/ line search(一维搜索,或线搜索)是最优化(Optimization)算法中的一个基础步骤/算法.它可以分为精确的一维搜索以 ...

  2. c++中结构体sort()排序

    //添加函数头 #include <algorithm> //定义结构体Yoy typedef struct { double totalprice;         //总价 doubl ...

  3. P1028 数的计算

    P1028 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n≤1000),然后对此自然数按照如下方法进行处理: 不作任何处理; 在它的左边加上一个自然数,但该 ...

  4. LEGB规则

    链接:https://www.cnblogs.com/GuoYaxiang/p/6405814.html 命名空间 大约来说,命名空间就是一个容器,其中包含的是映射到不同对象的名称.你可能已经听说过了 ...

  5. Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2

    A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...

  7. Ionic生命周期与注意点

    需要注意的地方是:在走页面的生命周期以前,会先走构造方法 构造方法只走一次,除非再次创建这个页面.所以如果先push了一个新页面,然后再调用pop()返回到之前的页面,那么是不会走构造方法里面的方法的 ...

  8. 【 HDU2966 】In case of failure(KD-Tree)

    BUPT2017 wintertraining(15) #5E HDU - 2966 题意 给平面直角坐标系下的n个点的坐标,求离每个点和它最近点的距离的平方.\(2 \le n \le 10^5\) ...

  9. 【BZOJ3157/3516】国王奇遇记(数论)

    [BZOJ3157/3516]国王奇遇记(数论) 题面 BZOJ3157 BZOJ3516 题解 先考虑怎么做\(m\le 100\)的情况. 令\(f(n,k)=\displaystyle \sum ...

  10. CSS之FLex布局介绍

    网页布局(layout)是CSS的一个重点应用. img 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如, ...