Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute script after ntpd sync

I have raspberry pi with raspbian. I would like to execute a script right after time gets synced with ntpd, my script needs correct datetime. How can i do that?

like image 224
davidovv Avatar asked Jan 22 '15 01:01

davidovv


People also ask

How long does NTP take to sync?

Usually it takes about five minutes (five good samples) until a NTP server is accepted as synchronization source. Interestingly, this is also true for local reference clocks that have no delay at all by definition.

How do I set NTP synchronized yes?

To enable time synchronization with an NTP server, do the following: In the Use NTP to set clock window, click Yes. The Configure NTP servers window opens. In the Configure NTP servers window, select New.

How use Ntpdate command in Linux?

Description. The ntpdate command sets the local date and time by polling the NTP servers specified to determine the correct time. It obtains a number of samples from each server specified and applies the standard NTP clock filter and selection algorithms to select the best of the samples.


2 Answers

The proper way to do this is to use ntp-wait. ntp-wait was tailor made for this type of situation. Here is the man page:

ntp-wait(1)                     User Commands                    ntp-wait(1)

NAME
       ntp-wait - Wait for ntpd to stabilize the system clock

SYNOPSIS
       ntp-wait [-flag [value]]... [--opt-name [[=| ]value]]...

       All arguments must be options.

DESCRIPTION
       will  send  at  most  num-tries queries to sleeping for secs-between-
       tries after each status return that says has not yet produced a  syn‐
       chronized and stable system clock.

       will do this quietly, unless the v flag is provided.

OPTIONS
       -n num-tries, --=num-tries
              Number  of  times to check ntpd.  This option takes an integer
              number as its argument.  The default num-tries for this option
              is:
                   100

              The  maximum  number  of times we will check ntpd to see if it
              has been able to synchronize and stabilize the system clock.

       -s secs-between-tries, --=secs-between-tries
              How long to sleep between tries.  This option takes an integer
              number  as  its  argument.  The default secs-between-tries for
              this option is:
                   6

              We will sleep for @file{secs-between-tries} after  each  query
              of ntpd that returns "the time is not yet stable".

       -v, -- Be verbose.

              By  default,  ntp-wait  is silent.  With this option, ntp-wait
              will provide status information.

       -?, --help
              Display usage information and exit.

       -!, --more-help
              Pass the extended usage information through a pager.

       - [{v|c|n}], --version[={v|c|n}]
              Output version of program and exit.  The default mode is  `v',
              a  simple version.  The `c' mode will print copyright informa‐
              tion and `n' will print the full copyright notice.


EXIT STATUS
       One of the following exit values will be returned:

       0      Successful program execution.

       1      The operation failed or the command syntax was not valid.
like image 71
dfc Avatar answered Sep 23 '22 15:09

dfc


Asuming that you have a user that has permissions to call ntpdate ( in other words, who can adjust the system's time), you could use the following script, I am using in the example below the ntp server "0.ca.pool.ntp.org"

#!/bin/bash

NEEDS_SYNC=1
while [ "$NEEDS_SYNC" -ne "0" ]; do
    ntpdate -t 4     0.ca.pool.ntp.org
    NEEDS_SYNC=$?    # If this variable is set ot 0, time sync worked
    sleep 2
done

# RUN THE SCRIPT THT NEEDS ntp SYNC'D TIME HERE

Note that you might need to install the package 'ntpdate' for this to work.

like image 25
Jorge Torres Avatar answered Sep 24 '22 15:09

Jorge Torres