0

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:

  1. Check if the service is configured properly and that is it running. Then, just exit from the script. i.e do nothing.
  2. If the service is configured properly, then just start service and exit from the script.
  3. 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
2
  • 1
    Shell variable names may only contain alphanumerics and underscores, and cannot start with a number. The variable you're attempting to assign on line 3 contains a dot, preventing the assignment (and any future references to it) from occurring successfully. Commented Apr 21, 2013 at 21:59
  • Thanks Michael for the info. I will change my script accordingly. Commented Apr 21, 2013 at 22:09

1 Answer 1

1

You mispelled a command: echo" should be echo " at line 77.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. It worked. Somehow I missed it and coudlnt even notice it. Thanks again, my code is working now.
Glad I could help! You should accept my answer and listen to michaelb advice in the comments to your question :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.