CentOS 7.5 under regular operating system update the system time

First, the pre-instructions

Under normal server can access the Internet: The Scene

Method: let it be updated every five minutes to achieve the system time by shell + crond

Two, shell scripts

Screenplay name: update_os_time.sh

Storage location: / server / scripts /

Note: Please check for ntpdate command, if not, install it with yum install ntpdate -y command

#!/bin/bash
#
# Define variables
RETVAL=0
Ntp_server=(
ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
)

# Determine the user to execute
if [ $UID -ne $RETVAL ];then
   echo "Must be root to run scripts"
   exit 1
fi

# Load locall functions library
[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# Install ntpdate command
yum install ntpdate -y >/dev/null 2>&1 

# for loop update os time
for((i=0;i<${#Ntp_server[*]};i++))
do
	/usr/sbin/ntpdate ${Ntp_server[i]} >/dev/null 2>&1 &
	RETVAL=$?
	if [ $RETVAL -eq 0 ];then
	   action "Update os time" /bin/true
	   break
	  else
	   action "Update os time" /bin/false
	   continue
	fi
done

# Scripts return values
exit $RTVAL

Third, the timing task provided

[root@node33 ~]# crontab -l|head -2
# Crond update os time. USER:chenliang TIME:2020-02-17
*/05 * * * * /bin/sh /server/scripts/update_os_time.sh >/dev/null 2>&1

 

Guess you like

Origin www.cnblogs.com/chenliangc/p/12324633.html