Mansome
Posts: 68
Joined: Sat May 16, 2015 2:32 pm

Command to run at startup after network has started

Fri May 05, 2017 7:59 pm

What is the easiest way to run sudo ntpd -s at startup but after the network has started to make sure the time is right every reboot?

epoch1970
Posts: 3895
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Command to run at startup after network has started

Fri May 05, 2017 8:06 pm

The easiest way is doing nothing...
NTPd can cope with the network going up or down. When it's up it will sync with other machines, when its down, it falls back to the local clock.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Mansome
Posts: 68
Joined: Sat May 16, 2015 2:32 pm

Re: Command to run at startup after network has started

Fri May 05, 2017 8:29 pm

It doesn't always work unless I run that command. Any reason why it isn't?

epoch1970
Posts: 3895
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Command to run at startup after network has started

Sat May 06, 2017 7:50 am

Mansome wrote:It doesn't always work unless I run that command. Any reason why it isn't?
Sure. NTPd's behaviour is conservative. It quits if the time delta between current system time and reference clock time is over something like 1000s. The daemon goes away, if you "pidof ntpd" you won't be able to find it running.

This condition can easily happen with the Pi which boots at the date it was last shut down (on Raspbian at least). Reboot and the clock is off by 2 secs, fine. Shelve the Pi for a week and the clock is off by a whole lot more seconds than NTPd dares to adjust automatically.

I'm not sure how others cope with it (I hate the guts of systemd so I've disabled timesyncd.) I use this recipe (mine) for now and it seems to work so far.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: Command to run at startup after network has started

Sat May 06, 2017 8:07 am

ntpd-option '-g' should help in this case:

Code: Select all

 -g     Normally, ntpd exits with a message to the  system  log  if  the
        offset  exceeds the panic threshold, which is 1000 s by default.
        This option allows the time to  be  set  to  any  value  without
        restriction; however, this can happen only once.  If the thresh‐
        old is exceeded after that, ntpd will exit with a message to the
        system log.  This option can be used with the -q and -x options.
Using this on several raspberry-systems and I'm happy with it ...

Best regards, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

User avatar
rpdom
Posts: 15615
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Command to run at startup after network has started

Sat May 06, 2017 8:38 am

The -g option is used when ntp starts during the system boot. The problem can occur when ntp tries to start before the network is available, in which case it gives up on the time syncing after a while. Then if the network comes back and the time is too far out, it won't sync time.

epoch1970
Posts: 3895
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Command to run at startup after network has started

Sat May 06, 2017 9:37 am

Agreed, for me "-g" is not enough on a Pi.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “General discussion”