Skip to contentSkip to conversation

ntp: added ntpdate support to the S49ntp script

PATCH New Oscar Gomez Fuente 2 participants 5 messages Oct 16, 2018 – Oct 17, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntp: added ntpdate support to the S49ntp script

---
 package/ntp/S49ntp | 58 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 24 deletions(-)
package/ntp/S49ntp +34 -24
index 35e5874..f5c4cb8 100755
@@ -1,34 +1,44 @@
 #! /bin/sh
 
 NAME=ntpd
+NTPDATE=/usr/bin/ntpdate
 
 # Read config file if it is present.
-if [ -r /etc/default/$NAME ]
-then
-  . /etc/default/$NAME
+if [ -r /etc/default/$NAME ]; then
+. /etc/default/$NAME
 fi
 
-case "$1" in
-  start)
-    printf "Starting $NAME: "
-    start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
-    [ $? = 0 ] && echo "OK" || echo "FAIL"
-    ;;
-  stop)
-    printf "Stopping $NAME: "
-    start-stop-daemon -K -q -n $NAME
-    [ $? = 0 ] && echo "OK" || echo "FAIL"
-    ;;
-  restart|reload)
-    echo "Restarting $NAME: "
-    $0 stop
-    sleep 1
-    $0 start
-    ;;
-  *)
-    echo "Usage: $0 {start|stop|restart|reload}" >&2
-    exit 1
-    ;;
+case $1 in
+start)
+	printf "Starting $NAME: "
+	if [ -f $NTPDATE ]; then
+		NUM_SERVER=0
+		CURRENT_DATE=$(date | grep "1970")
+		while [ "$NUM_SERVER" != "4" -a "$CURRENT_DATE" != "" ]
+		do
+			$NTPDATE $NUM_SERVER.pool.ntp.org > /dev/null 2>&1
+			CURRENT_DATE=$(date | grep "1970")
+			NUM_SERVER=$(( $NUM_SERVER + 1 ))
+		done
+	fi
+	start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
+	[ $? = 0 ] && echo "OK" || echo "FAIL"
+	;;
+stop)
+	printf "Stopping $NAME: "
+	start-stop-daemon -K -q -n $NAME
+	[ $? = 0 ] && echo "OK" || echo "FAIL"
+	;;
+restart|reload)
+	echo "Restarting $NAME: "
+	$0 stop
+	sleep 1
+	$0 start
+	;;
+*)
+	echo "Usage: $0 {start|stop|restart|reload}" >&2
+	exit 1
+	;;
 esac
 
 exit 0
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 ntp: added ntpdate support to the S49ntp script

Oscar,

Quoted 40 lines · Oscar Gomez Fuente
---
 package/ntp/S49ntp | 58 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 24 deletions(-)
package/ntp/S49ntp +3 -19
index 35e5874..f5c4cb8 100755
@@ -1,34 +1,44 @@
 #! /bin/sh
 
 NAME=ntpd
+NTPDATE=/usr/bin/ntpdate
 
 # Read config file if it is present.
-if [ -r /etc/default/$NAME ]
-then
-  . /etc/default/$NAME
+if [ -r /etc/default/$NAME ]; then
+. /etc/default/$NAME
 fi
 
-case "$1" in
-  start)
-    printf "Starting $NAME: "
-    start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
-    [ $? = 0 ] && echo "OK" || echo "FAIL"
-    ;;
-  stop)
-    printf "Stopping $NAME: "
-    start-stop-daemon -K -q -n $NAME
-    [ $? = 0 ] && echo "OK" || echo "FAIL"
-    ;;
-  restart|reload)
-    echo "Restarting $NAME: "
-    $0 stop
-    sleep 1
-    $0 start

package/ntp/S49ntp line 15

Quoted 10 lines
package/ntp/S49ntp +5 -5
-    ;;
-  *)
-    echo "Usage: $0 {start|stop|restart|reload}" >&2
-    exit 1
-    ;;
+case $1 in
+start)
+       printf "Starting $NAME: "
+       if [ -f $NTPDATE ]; then
+               NUM_SERVER=0

I'd suggest checking if there was a preferred time server you should use in /etc/default/$NAME before assuming using the pool. I don't believe there is a defined syntax for that config's variables as it is different from the actual ntp.conf. You can probably just assume a variable name like NTP_SERVER.

Quoted 32 lines
package/ntp/S49ntp +26 -1
+               CURRENT_DATE=$(date | grep "1970")
+               while [ "$NUM_SERVER" != "4" -a "$CURRENT_DATE" != "" ]
+               do
+                       $NTPDATE $NUM_SERVER.pool.ntp.org > /dev/null 2>&1
+                       CURRENT_DATE=$(date | grep "1970")
+                       NUM_SERVER=$(( $NUM_SERVER + 1 ))
+               done
+       fi
+       start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
+       [ $? = 0 ] && echo "OK" || echo "FAIL"
+       ;;
+stop)
+       printf "Stopping $NAME: "
+       start-stop-daemon -K -q -n $NAME
+       [ $? = 0 ] && echo "OK" || echo "FAIL"
+       ;;
+restart|reload)
+       echo "Restarting $NAME: "
+       $0 stop
+       sleep 1
+       $0 start
+       ;;
+*)
+       echo "Usage: $0 {start|stop|restart|reload}" >&2
+       exit 1
+       ;;
 esac
 
 exit 0
--

1.9.1

Signature · 8 lines
Matthew L Weber / Pr Software Engineer
Airborne Information Systems / RC Linux Secure Platforms
MS 131-100, C Ave NE, Cedar Rapids, IA, 52498, USA
www.rockwellcollins.com

Note: Any Export License Required Information and License Restricted
Third Party Intellectual Property (TPIP) content must be encrypted and
sent to matthew.weber at corp.rockwellcollins.com.
#2 lore raw

PATCH 1/1 ntp: added ntpdate support to the S49ntp script

Hi Matthew,

Quoted 5 lines

I'd suggest checking if there was a preferred time server you should use in /etc/default/$NAME before assuming using the pool. I don't believe there is a defined syntax for that config's variables as it is different from the actual ntp.conf. You can probably just assume a variable name like NTP_SERVER.

Yes, it is a great idea. What do you think about that?

----->
#! /bin/sh

NAME=ntpd
NTPDATE=/usr/bin/ntpdate
MAX_NUM_SERVERS=3
NTP_NAME_SERVER=pool.ntp.org

# Read config file if it is present.
if [ -r /etc/default/$NAME ]; then
. /etc/default/$NAME
fi

case $1 in
start)
printf "Starting $NAME: "
if [ -f $NTPDATE ]; then
CURRENT_DATE=$(date | grep "1970") if [ -r /etc/default/$NAME ]; then
$NTPDATE $NTP_SERVER > /dev/null 2>&1 CURRENT_DATE=$(date | grep "1970")
fi
NUM_SERVER=0
while [ $NUM_SERVER -le $MAX_NUM_SERVERS -a
"$CURRENT_DATE" != "" ]

                do
                        $NTPDATE $NUM_SERVER.$NTP_NAME_SERVER > /dev/null 2>&1
                        NUM_SERVER=$(( $NUM_SERVER + 1 ))
                        CURRENT_DATE=$(date | grep "1970")
                done
        fi
        start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
        [ $? = 0 ] && echo "OK" || echo "FAIL"
        ;;

stop)
printf "Stopping $NAME: "
start-stop-daemon -K -q -n $NAME
[ $? = 0 ] && echo "OK" || echo "FAIL"
;;
restart|reload)

        echo "Restarting $NAME: "
        $0 stop
        sleep 1
        $0 start
        ;;
*)
        echo "Usage: $0 {start|stop|restart|reload}" >&2
        exit 1
        ;;

esac

exit 0

----->

Best regards.

Oscar Gomez Fuente
TST Sistemas
www.tst-sistemas.es

#3 lore raw

PATCH 1/1 ntp: added ntpdate support to the S49ntp script

Hi everyone,

I made some improvements. If everyone thinks it is ok I will send a patch:
----->
#! /bin/sh

NAME=ntpd
NTPDATE=/usr/bin/ntpdate
MAX_NUM_SERVERS=3
NTP_NAME_SERVER=pool.ntp.org

# Read config file if it is present.
if [ -r /etc/default/$NAME ]; then
. /etc/default/$NAME
fi

case $1 in
start)

    printf "Starting $NAME: "
    CURRENT_DATE=$(date | grep "1970")
    if [ -f $NTPDATE -a "$CURRENT_DATE" != "" ]; then
        if [ "$NTP_SERVER" != "" ]; then
            $NTPDATE $NTP_SERVER > /dev/null 2>&1
            CURRENT_DATE=$(date | grep "1970")
        fi
        NUM_SERVER=0
        while [ $NUM_SERVER -le $MAX_NUM_SERVERS -a "$CURRENT_DATE" != "" ]
        do
            $NTPDATE $NUM_SERVER.$NTP_NAME_SERVER > /dev/null 2>&1
            NUM_SERVER=$(( $NUM_SERVER + 1 ))
            CURRENT_DATE=$(date | grep "1970")
        done
    fi
    start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;

stop)
printf "Stopping $NAME: "
start-stop-daemon -K -q -n $NAME
[ $? = 0 ] && echo "OK" || echo "FAIL"
;;
restart|reload)

    echo "Restarting $NAME: "
    $0 stop
    sleep 1
    $0 start
    ;;
*)
    echo "Usage: $0 {start|stop|restart|reload}" >&2
    exit 1
    ;;

esac

exit 0

----->

Best regards.

#4 lore raw

PATCH 1/1 ntp: added ntpdate support to the S49ntp script

Oscar,

Quoted 9 lines · Oscar Gomez Fuente

Hi everyone,

I made some improvements. If everyone thinks it is ok I will send a patch:
----->
#! /bin/sh

NAME=ntpd
NTPDATE=/usr/bin/ntpdate

Do you need to try more then one server?

I'd suggest the following instead which handles the default NTP_SERVER without a conditional and removes the loop.

NTP_SERVER=0.pool.ntp.org

Quoted 11 lines

# Read config file if it is present.
if [ -r /etc/default/$NAME ]; then
. /etc/default/$NAME
fi

case $1 in
start)

    printf "Starting $NAME: "
    CURRENT_DATE=$(date | grep "1970")
    if [ -f $NTPDATE -a "$CURRENT_DATE" != "" ]; then
            $NTPDATE $NTP_SERVER > /dev/null 2>&1
            [ $?  = 1 ] && echo -n "(No initial time set)"
Quoted 26 lines
    fi
    start-stop-daemon -S -q -x /usr/sbin/ntpd -- -g
    [ $? = 0 ] && echo "OK" || echo "FAIL"
    ;;

stop)
printf "Stopping $NAME: "
start-stop-daemon -K -q -n $NAME
[ $? = 0 ] && echo "OK" || echo "FAIL"
;;
restart|reload)

    echo "Restarting $NAME: "
    $0 stop
    sleep 1
    $0 start
    ;;
*)
    echo "Usage: $0 {start|stop|restart|reload}" >&2
    exit 1
    ;;

esac

exit 0

----->

Best regards.

Signature · 8 lines
Matthew L Weber / Pr Software Engineer
Airborne Information Systems / RC Linux Secure Platforms
MS 131-100, C Ave NE, Cedar Rapids, IA, 52498, USA
www.rockwellcollins.com

Note: Any Export License Required Information and License Restricted
Third Party Intellectual Property (TPIP) content must be encrypted and
sent to matthew.weber at corp.rockwellcollins.com.