#! /bin/bash # It is vitally important that server time would be correct during server install/upgrade. This script synchronizes system clock with one of internet ntp time servers # # Arguments: # # $1 - FIRST_INSTALL # if this argument is present the script immeadiately exits if any failure occours this way aborting further installation # NO ARGUMENTS # will try to synchronize the time, will report { FAILED, OK } silently # Rocky9 compatible FIRST_INSTALL="$1" . /usr/src/k_framework/main.sh VERSION="1.1.1" SCRIPT_NAME="Time Sync" k_start k_detect_os if [[ $ROCKY9 == 1 ]]; then if [ ! -f /usr/sbin/chronyd ]; then yum -y install chronyd else report "chronyd is installed" 0 fi if k_service_is_running chronyd; then report "chronyd service is running" 0 else report "chronyd service is not running" 2 k_service_start chronyd if k_service_is_running chronyd; then report "chronyd service is running" 0 else report "chronyd service is not running. Failed to start, solve manually" 1 k_exit 1 fi fi if k_service_is_enabled chronyd; then report "chronyd service is enabled" 0 else report "chronyd service is disabled" 2 k_service_enable chronyd if k_service_is_enabled chronyd; then report "chronyd service is enabled" 0 else report "chronyd service is disabled. Failed to enable, solve manually" 1 k_exit 1 fi fi # force sync time service chronyd stop chronyd -q 'pool pool.ntp.org iburst' service chronyd start date k_exit $EXIT_CODE fi # Other OS, not Rocky9 if [ ! -f /usr/sbin/ntpd ]; then yum -y install ntp fi if [ -f /usr/sbin/ntpdate ]; then k_service_stop ntpd /usr/sbin/ntpdate ntp.ubuntu.com &> /dev/null #hiding any output k_service_start ntpd.service else report "Failed to install ntpdate command which synchronizes your clock with one of the internet time servers. Check server internet connection or contact the Kolmisoft team." 1 if [ "$FIRST_INSTALL" == "FIRST_INSTALL" ]; then echo -e "\nThis type of problem WILL LEAD to serious problems during installation, aborting installation.\n"; fi k_exit 1 fi # Check if ntpd starts after server is restarted if ! k_service_is_enabled ntpd; then k_service_enable ntpd k_service_restart ntpd report "Added ntpd service to autostart" 3 rm -rf /etc/cron.d/ntpdate &> /dev/null# no longer needed. fi k_service_stop ntpd /usr/sbin/ntpdate pool.ntp.org &> /dev/null #hiding any output k_service_start ntpd if [ "$?" == "0" ]; then report "NTP: time synchronized successfully" 0 k_exit 0 else #if fails if hash ntpd 2> /dev/null then #maybe ntpd daemon is interfering with ntpdate? k_service_stop ntpd &> /dev/null #hiding any output /usr/sbin/ntpdate ntp.ubuntu.com &> /dev/null #hiding any output STATUS="$?" k_service_start ntpd.service &> /dev/null #hiding any output if [ "$STATUS" == "0" ]; then report "NTP: time synchronized successfully" 0 k_exit 0 else report "Failed to synchronize your clock with one of the internet time servers, this could lead to serious problems, aborting installation." 1 if [ "$FIRST_INSTALL" == "FIRST_INSTALL" ]; then k_exit 1; fi fi else report "Failed to synchronize your clock with one of the internet time servers, this could lead to serious problems, aborting installation." 1 if [ "$FIRST_INSTALL" == "FIRST_INSTALL" ]; then k_exit 1; fi fi fi k_exit $EXIT_CODE