Shell 同步时间脚本
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 同步时间脚本的更多相关文章
- linux 下使rdate命令支持ipv6 ntp server 同步时间
如果使用linux 下,busybox自带的rdate命令 去ipv6 的ntp server 同步时间的话,会提示invalid argument :无效参数. 那么现在下载rdate的源码并对其进 ...
- 03: shell简单监控脚本
1.1 监控apache web server #! /bin/bash # apache netstat -anpt | grep 80 &> /dev/null if [ $? -e ...
- Shell编程-01-Shell脚本初步入门
目录 什么是Shell 什么是Shell脚本 Shell脚本语言的种类 常用操作系统默认Shell Shell 脚本的建立和执行 脚本规范 什么是Shell 简单来说Shell其实就是一个命令 ...
- 生产力工具:shell 与 Bash 脚本
生产力工具:shell 与 Bash 脚本 作者:吴甜甜 个人博客网站: wutiantian.github.io 注意:本文只是我个人总结的学习笔记,不适合0基础人士观看. 参考内容: 王顶老师 l ...
- Shell日期时间和时间戳的转换
Gitlab的备份文件是以时间戳显示的,类似:1438624820_gitlab_backup.tar 为了更易于阅读,想把文件名转换成日期格式:2015-08-04_gitlab_backup.ta ...
- python第三方库推荐 - 通过ntplib在windows上同步时间
很多时候我们有通过程序脚本同步校正北京时间的需求. 在linux上同步时间比较方便,安装个ntpdate软件就行了. 但是在windows的要同步时间比较麻烦. 这时想到的就是从网络获取一个准确的时间 ...
- shell获取时间的相关命令
Linux shell获取时间和时间间隔(ms级别) 说明:在进行一些性能测试的时候,有时候我们希望能计算一个程序运行的时间,有时候可能会自己写一个shell脚本方便进行一些性能测试的控制(比如希望能 ...
- Raspberry Pi开发之旅-同步时间
使用htpdate同步时间 由于树莓派板子上没有 RTC 硬件和电池,因此树莓派上的系统时间重启是保存不了的.网上已经有人想到应对 NTP 被防火墙封掉类似的需求了,开源的 htpdate 命令直接使 ...
- Linux离线同步时间
Linux离线同步时间 思路:以其中一台时间为准 脚本 #!/bin/shcurrent=`date '+%H:%M:%S'` for i in bigdata1 bigdata2 bigdata3 ...
随机推荐
- 如何在虚拟机下配置centOS7
链接地址:https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for=pc
- JS 强制类型转化
在Js中, 强制类型转化分为两种情况: 一种是引用类型转化基本类型, 如数组转化成数字:一种是两种不同基本类型之间的转化,如字符串转化为数字.你不能将基本类型转化成引用类型,比如,不可能把数字转化为数 ...
- 搭建Hexo博客(四)-设置
1.主题 比较流行的是yilia,下载主题到theme目录即可.再根据说明文档进行设置. 2.图片发布 source下创建的目录,并且目录下有文件,则文件会被生成到post目录中.图片可以放在这里.既 ...
- python--使用pymyslq操作数据库
1.安装pymysql 在命令行内输入 pip install pymysql : 2.pycharm连接mysql 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动. 你有可 ...
- .net core 2.0 Unable to convert MySQL date/time to System.DateTime
解决方案 在连接字符串加入convert zero datetime=True
- P1308 统计单词数
P1308 题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请 ...
- IDEA+Maven+多个Module模块(创建多模块SpringBoot整合项目)
最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...
- Catch the Theves HDU - 3870(s - t平面图最小割)
题意: 板题...建个图..跑一遍spfa就好了...嘻嘻... 注意..数组大小就好啦..400 * 400 = 1600 我也是抑郁了..沙雕的我.. #include <iostream& ...
- git 本地推送远程仓库报错: error: failed to push some refs to 'https://github.com/yangtuothink/mxonline.git'
报错现象 添加远程仓库后 推送代码的时候报错 报错分析 远程代码和本地代码不匹配问题 远程初始仓库的创建有些默认 的 README什么的本地是没有的 需要先同步后再上传 报错解决 git push - ...
- Breakable loop in Scratch
Breakable loop in Scratch https://stackoverflow.com/questions/30682144/breakable-loop-in-scratch Bre ...