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 ...
 
随机推荐
- Quick Introduction to SQL Server Profiler
			
Introduction to Profiler SQL Server Profiler — or just Profiler — is a tool that can help monitor al ...
 - Nginx 4层反向代理
			
L112 是基于TCP POST_ACCEPT阶段 在建立连接后所做的事情 PREACCESS阶段 limit_conn 限流 与HTTP类似 ACCESS阶段 类似HTTP模块用于控制访问权限 S ...
 - docker --环境变量制作模板
			
比如你现在有配置文件:decoder.conf decoder.conf里面有两个配置项写的是: THREAD_NUM:24 GPU_ID:0 现在你希望通过某个统一的配置来修改这些配置项,以免在部署 ...
 - python 项目启动路径自动添加
			
import os import sys base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #找到当前项目 ...
 - Flask 构建微电影视频网站(一)
			
Flask构建电影视频网站 Python MTV模型 Flask微内核 Flask扩展插件配置及使用方法 根据业务开发网站前后台功能 Flask结合MySQL数据库 你将可以独立开发网站 独立部署运维 ...
 - 接口压测初识java GC
			
1.先用Spring Boot 搭建 web 服务,构建api 服务 @RequestMapping("/index") @ResponseBody public String i ...
 - 一种HBase表数据迁移方法的优化
			
1.背景调研: 目前存在的hbase数据迁移主要分如下几类: 根据上图,可以看出: 其实主要分为两种方式:(1)hadoop层:因为hbase底层是基于hdfs存储的,所以可以通过把hdfs上的数据拷 ...
 - Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
			
Go Deeper Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
 - 【BZOJ3997】【TJOI2015】组合数学 Dilworth定理 DP
			
题目描述 有一个\(n\times m\)的网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完. 此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子 ...
 - jqGrid 手册 -  搜索
			
搜索 类型: colModel Options Toolbar Searching Custom Searching Single field searching Advanced Searching ...