linux自动同步时间centos服务器时间同步简单即用版

作为日常维护测环境的一个螺丝钉,多次被抱怨服务器时间差异导致异常。

之前每次接到类似反馈,直接在对应服务器执行

ntpdate -u cn.pool.ntp.org   # 手动同步时间,执行这个就完了(所有服务器都执行这个)

今天发横要做一个定时自动同步时间,简略记录一下:

1.编辑文件vim /etc/crontab

文件内容如下,注释的内容有对每个字段的解释:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# For details see man 4 crontabs

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed

# */1 * * * * root /usr/bin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1

59 23 */1 * * root ntpdate -u cn.pool.ntp.org >> /home/temptime.log

简单翻译一下就是:

    前5个*代表的是时间,之后是用户,之后是要执行的命令。就这么三段

    时间注释表示得比较清楚,如果是类似*/1,比如说第一个*是这样,其它都是*没有别的,那就是一分钟执行一次

    59 23 */1 * * root ntpdate -u cn.pool.ntp.org >> /home/temptime.log    # 这表示每天的23点59分,用户root会执行ntpdate -u cn.pool.ntp.org >> /home/temptime.log这条命令

2.文件修改保存以后,重启一下就生效了

systemctl restart crond.service 


如果没有cronb、ntpdate,可以用yum安装哦

安装crond

yum -y install vixie-cron

yum -y install crontabs

参考资料:https://www.cnblogs.com/chenmh/p/5430258.html

tip1:crond加入开启启动服务:chkconfig crond on

tip2:查看定时任务是否启动:service crond status

『『重点:需环境搭建实践资料、pipeline项目源码\交流学习添加v:ct-xiaozhou

免费提供的。行业变迁,多个好友多点可能性。非机构』』

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值