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. P1880 [NOI1995]石子合并 区间dp+拆环成链

    思路 :一道经典的区间dp  唯一不同的时候 终点和起点相连  所以要拆环成链  只需要把1-n的数组在n+1-2*n复制一遍就行了 #include<bits/stdc++.h> usi ...

  2. Suffix

    $ 题目描述 给定一个序列\(A\),请你输出\(\sum_{1< i< j < k < h}A_iA_jA_kA_h(mod ~~1e9+7)\) \(Solution\) ...

  3. bzoj 2957 楼房重建 (线段树+思路)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2957 思路: 用分块可以很简单的过掉,但是这道题也可以用线段树写. 分类讨论左区间最大值对 ...

  4. Spring学习记录

    Java类定义配置@Configuration //标记为配置类@ComponentScan //标记为扫描当前包及子包所有标记为@Component的类@ComponentScan(basePack ...

  5. ubuntu “无法获得锁 /var/lib/dpkg/lock -open”

    在ubuntu系统终端下,用apt-get install 安装软件的时候,如果在未完成下载的情况下将终端中断,此时 apt-get进程可能没有结束.结果,如果再次运行apt-get install ...

  6. C# 新语法收集

    内联变量 使用int.tryparst时,先要申明变量,用于out参数 int d; int.tryparse(s,out d); 使用内联变量写法可以如下.功能一样简化了写化 int.trypars ...

  7. 编写高质量代码:改善Java程序的151个建议 --[98~105]

    建议的采用顺序是List中泛型顺序依次为T.?.Object (1).List是确定的某一个类型 List表示的是List集合中的元素都为T类型,具体类型在运行期决定:List<?>表示的 ...

  8. 区块链使用Java,以太坊 Ethereum, web3j, Spring Boot

    Blockchain is one of the buzzwords in IT world during some last months. This term is related to cryp ...

  9. Spring boot中使用aop详解

      版权声明:本文为博主武伟峰原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu.       aop是spring的两大功能模块之一,功能非常强大,为解 ...

  10. 阻止 form 回车 自动提交

    问题:当form表单中只有一个input时,在input中按回车键会自动提交. 解决方案: 1.form元素上加onsubmit="return false"(推荐) 2.多个in ...