Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am currently trying to cut boot time for an embedded Linux system. One way we figured we could do that is by postponing the ntp init.d script.

The daemon tried to synchronize with the server via the ethernet connection. But we don't always have that connection up and it takes a long time to boot while it tries to synchronize. In our system, ntp is not crucial, so it can be started in a later time.

What I wanted to do was to make the ntpd init.d script to wait for like 5 minutes but without blocking the rest of the boot operation.

This is the S49 in the init.d folder I'm using:

    #! /bin/sh
    #
    # System-V init script for the openntp daemon
    #

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DESC="network time protocol daemon"
    NAME=ntpd
    DAEMON=/usr/sbin/$NAME
    NTPDATE_BIN=/usr/bin/ntpdate

    # Gracefully exit if the package has been removed.
    test -x $DAEMON || exit 0

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

    case "$1" in
    start)
        if [ -x $NTPDATE_BIN ] ; then
            echo -n "Getting initial time via ntp"
            $NTPDATE_BIN $NTPDATE_OPTS $NTPSERVERS > /dev/null 2>&1
            echo "."
        fi

        echo -n "Starting $DESC: $NAME"
        start-stop-daemon -S -q -x $DAEMON
        echo "."
        ;;
    stop) echo -n "Stopping $DESC: $NAME"
        start-stop-daemon -K -q -n $NAME
        echo "."
        ;;
    reload|force-reload) echo -n "Reloading $DESC configuration..."
        start-stop-daemon -K -q -n $NAME -s 1
        echo "done."
            ;;
    restart) echo "Restarting $DESC: $NAME"
        $0 stop
        sleep 1
        $0 start
        ;;
    *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
    esac

    exit 0
share|improve this question
    
Would you consider an event based init serverice? –  Braiam May 13 at 15:18
1  
What OS? ntpd should be backgrounding itself, and should not hang anything. This isn't some custom init script is it? –  Patrick May 13 at 15:18
    
I don't think it's up to me to change the init service. And I'm not sure whether the script is custom or not. –  franchzilla May 13 at 15:38
1  
Ditto Patrick. Unless you've done some work to create such a situation yourself, the boot process will not stop and wait for ntpd to get a connection. It will just start the daemon in the background. So either you need to explain what you've done to make it like that, or why you believe that is happening if you didn't. –  goldilocks May 13 at 15:39
1  
@franchzilla Are you starting the ntp daemon, or are you doing a one-time time sync on boot? The one-time sync will pause boot. You could be doing a one-time sync with ntpdate or ntpd -q –  derobert May 13 at 15:46

2 Answers 2

up vote 3 down vote accepted

As your init script shows, your delay is being caused by ntpdate when it starts.

This is one of the most common errors when setting up NTP. The purpose of using ntpdate is so that ntpd won't exit with an error when it starts up. It does this if it detects that the time difference is very large. However ntpd has the ability to ignore this time difference just once. So by turning this on, ntpdate is no longer necessary.

The solution then is to disable ntpdate and use this feature.

To do this, all you need to do is create /etc/default/ntpd with the following:

NTPDATE_BIN="-"
DAEMON="/usr/sbin/ntpd -g"

The NTPDATE_BIN="-" is for this bit of the init script:

if [ -x $NTPDATE_BIN ] ; then

It will look to see if there is a file called - which is executable, which there isn't, so it will skip that section.
 

The DAEMON="/usr/sbin/ntpd -g" is for this:

start-stop-daemon -S -q -x $DAEMON

It will cause ntpd to be invoked as ntpd -g. The -g option is the option that tells it allow the large time difference.

share|improve this answer
case "$1" in
start)
    if [ -x $NTPDATE_BIN ] ; then
        echo -n "Getting initial time via ntp"
        $NTPDATE_BIN $NTPDATE_OPTS $NTPSERVERS > /dev/null 2>&1
        echo "."
    fi

ntpdate is getting executed at boot independently of everything if it's found as executable. If you want to stop this behavior you should remove ntpdate using the package manager you have or manually moving the binary.

Modifying the script is also an option, but if you update it's probably that it would get overwritten.

share|improve this answer
    
But is there some way to make ntpdate run after 5 minutes this script is run without blocking the rest of the boot up? –  franchzilla May 13 at 16:49
    
@franchzilla since you don't want the time to be updated at boot disabling it and leaving the daemon take care of the time sync seems like the wisest approach instead of trying to delaying. Remember that SysV init scripts are extremely limited in functionality. Delaying scripts without blocking the rest when having the exact time at boot time isn't a priority is trying to solve the problem using the wrong approach. –  Braiam May 13 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.