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

224 阅读1分钟

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

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

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

参考资料:www.cnblogs.com/chenmh/p/54…

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

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

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

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