I have written a script which checks the ntp service. It updates it and then starts the service.
There are three things I'm doing in the script below:
- Check if the service is configured properly and that is it running. Then, just exit from the script. i.e do nothing.
- If the service is configured properly, then just start service and exit from the script.
- If the service is not configured, then stop the service -> configure service-> start service.
From above scenarios, scenario-1 and scenario-3 work fine. But while testing the scenario-2, it throws the error below:
~ # sh ntpConfigure.sh ntpConfigure.sh: line 3: NTP_CONF.BAK=/etc/ntp.conf.backup: not found ntpd is not running /etc/init.d/ntpd is NOT running server time.myCompany.com /etc/ntp.conf is configured Service is configured already, starting ntp service Starting ntpd ntpConfigure.sh: line 77: echo Service is started successfully: not found
Any idea why is it throwing this error? I checked everything, but my code looks good. Functionality works fine with the above error. Any ideas?
code:
#VERIFY_NTPQ="/bin/ntpq -p"
NTP_CONF="/etc/ntp.conf"
NTP_CONF.BAK="/etc/ntp.conf.backup"
NTPQ_SERVICE="/etc/init.d/ntpd"
TOUCH="/bin/touch"
GREP="/bin/grep"
CP="/bin/cp"
CHKCONFIG="/bin/chkconfig"
$NTPQ_SERVICE status
if [ $? -eq 0 ]
then
isSERVICE_RUNNING=true
echo "$NTPQ_SERVICE is running"
else
isSERVICE_RUNNING=false
echo "$NTPQ_SERVICE is NOT running"
fi
$TOUCH $NTP_CONF
$GREP "server" $NTP_CONF
if [ $? -eq 0 ]
then
isSERVICE__CONFIGURED=true
echo "$NTP_CONF is configured"
else
isSERVICE__CONFIGURED=false
echo "$NTP_CONF is NOT configured"
fi
if $isSERVICE_RUNNING && $isSERVICE__CONFIGURED
then
echo "ntp service is already configured, nothing to do"
exit 0
elif $isSERVICE__CONFIGURED
then
echo "Service is configured already, starting ntp service"
$NTPQ_SERVICE start
if [ $? -eq 0 ]
then
echo" Service is started successfully"
exit 0
else
echo "Failed to start service"
exit 1
fi
else
echo "$NTP_CONF not configured, Configuring $NTP_CONF"
echo "Stopping ntp service "
$NTPQ_SERVICE stop
echo "Taking backup of $NTP_CONF file to $NTP_CONF.BAK"
$CP $NTP_CONF $NTP_CONF.BAK
echo "Updating $NTP_CONF file with the required configurations"
echo "restrict 127.0.0.1" > $NTP_CONF
echo "restrict default kod nomodify notrap" >> $NTP_CONF
echo "driftfile /etc/ntp.drift" >> $NTP_CONF
echo "server time.myCompany.com" >> $NTP_CONF
if [ $? -eq 0 ]
then
echo "$NTP_CONF is configued properly"
echo "Starting ntp service"
$NTPQ_SERVICE start
if [ $? -eq 0 ]
then
echo "ntp service configured and started successfully"
echo "Configuring ntp service to start at bootup"
$CHKCONFIG ntpd on
exit 0
else
echo "Failed to start ntp service"
exit 1
fi
else
echo "Failed to configure ntp service"
exit 1
fi
fi