4

I'm using an RPi with Raspbian which runs the NTP-daemon by default. I have connected an rtc-ds1307 external i2c hardware clock. I was following the instructions given here to set it up:

http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi/set-rtc-time

After removing the default fake-hwclock from the system, basically everything worked fine. When I boot the RPi without network connection, the time from the external hwclock is copied to the system clock. When there is Internet connection the NTP-daemon updates the system time afterwards. When I regularly shut down the RPi, the Internet-time is copied to the external hwclock. But in case of a power out, the old time remains on it.

Is there a simple way to always check the difference between the hw-clock and the time-server. I mean, if the time-server can be accessed and there is a certain difference between hw-clock and the time-server, then the hw-clock should be updated. If there is no Internet connection, the system anyway relies on the hw-clock after booting.

thx

  • why you need NTP and network time when you have hardware RTC clock? just use the time you read from ds1307 and forget about NTP, your time readings will be quite precise for the next 10 years or so. – lenik Sep 26 '13 at 23:37
  • @lenik: maybe you are right. I just thought, if I can get the perfect time from NTP, why not keeping the RTC synchronized. – Thomas Sep 28 '13 at 9:36
3

The hwclock utility has a switch --systohc which will set the hardware clock to the current system time.
You should be able to script it so that it saves the time each time it connects to the internet

  • Yes, I know. It is the same like "hwclock -w". But my question was about how to automate this process. I thought maybe the NTP daemon can do this for me. I mean, it keeps the system clock synchronized, why not also hw-clock. I would love to have a tool that only checks the difference between hwclock and NTP, and in case of a difference, the hwclock should be updated. That would be nice. thx – Thomas Sep 28 '13 at 9:40
  • Which NTP daemon are you using ? – Lawrence Sep 28 '13 at 15:34
  • The standard ntpd which comes with Raspbian. – Thomas Sep 28 '13 at 17:25
  • It doesn't look like the standard ntp daemon can do any sort of automation, so you'll need to script it. cronjob to run every 5 minutes would be fine. – Lawrence Sep 29 '13 at 7:23
  • Yes it seems so. Thx. Thats also the plan I'm working on right now. – Thomas Sep 29 '13 at 11:01
3

To answer the question myself. I made a shell-script cronjob for updating the hwclock in case of a time difference. I have also added a log-file to enter changes. You need to run the script in your preferred interval (I'm using a couple of minutes). If someone is interested in my solution, here is the code:

#!/bin/bash

# Location of logfile
LOGFILE="/home/pi/hwcsync/ntplog.log"

# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock
maxDiffSec="2"

msgNoConnection="No connection to time-server"
msgConnection="Connection to time-server"

# Check for NTP connection
if ( ntpq -p | grep -q "^*"  ); then
        echo $msgConnection
        echo "---------------------------------"

        secHwClock=$(sudo hwclock --debug | grep "^Hw clock time" | awk '{print $(NF-3)}')
        echo "HwClock: $secHwClock sec"

        secSysClock=$(date +"%s")
        echo "SysClock: $secSysClock sec"
        echo "---------------------------------"

        secDiff=$(($secHwClock-$secSysClock))

        # Compute absolute value
        if ( echo $secDiff | grep -q "-" ); then
            secDiff=$(echo $secDiff | cut -d "-" -f 2)
        fi

        echo "Difference: $secDiff sec"

        msgDiff="HwClock difference: $secDiff sec"
        if [ "$secDiff" -gt "$maxDiffSec" ] ; then
                echo "---------------------------------"
                echo "The difference between Hw- and Sys-Clock is more than $maxDiffSec sec."
                echo "Hw-Clock will be updated"

                # Update hwclock from system clock
                sudo hwclock -w
                msgDiff="$msgDiff --> HW-Clock updated."
        fi
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgConnection") || [ "$secDiff" -gt "$maxDiffSec" ]; then
                echo $(date)": "$msgConnection". "$msgDiff >> $LOGFILE
        fi
else
        # No NTP connection
        echo $msgNoConnection
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgNoConnection"); then
                echo $(date)": $msgNoConnection" >> $LOGFILE
        fi
fi
  • you should really have this on github – Phill Pafford Aug 23 '14 at 4:23
  • great script&idea but i get an error executing it " sh clocksync.sh : not foundh: 2: clocksync.sh: : not foundh: 5: clocksync.sh: : not foundh: 8: clocksync.sh: : not foundh: 11: clocksync.sh: clocksync.sh: 52: clocksync.sh: Syntax error: "fi" unexpected (expecting "then") " Thanks a lot – Adrien S. Oct 10 '18 at 15:34
0

For information, on my pi if I wait more than 15mn the ntp time is saved on the hwclock.

So no need for a script if 15mn is good for you.

  • this is a comment, not an actual answer. – jitter Jun 3 '17 at 21:33
-1

I would add auto create log file

# Location of logfile
LOGFILE="/var/log/ntplog.log"

if [ ! -f $LOGFILE ]; then
  touch $LOGFILE
fi

# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock
maxDiffSec="2"

I have tested this script and it runs great ;-)

  • What would this actually do in the context of the hardware clock/NTP stuff? – Fred Apr 20 '14 at 18:31
  • thanks - useful addition. please consider using comments instead of adding another answer. – jitter Jun 3 '17 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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