Search code examples
linuxraspberry-pintpd

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?


Solution

  • 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.