5

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

Ghanima
  • 15,958
  • 17
  • 65
  • 125
Thomas
  • 81
  • 1
  • 1
  • 7

4 Answers4

4

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

Lawrence
  • 2,692
  • 15
  • 14
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 --verbose | 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

Thomas
  • 81
  • 1
  • 1
  • 7
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.

Axel C
  • 11
-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 ;-)