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:


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.


  • 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, 2013 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, 2013 at 9:36

4 Answers 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

  • 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, 2013 at 9:40
  • Which NTP daemon are you using ?
    – Lawrence
    Sep 28, 2013 at 15:34
  • The standard ntpd which comes with Raspbian.
    – Thomas
    Sep 28, 2013 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, 2013 at 7:23
  • Yes it seems so. Thx. Thats also the plan I'm working on right now.
    – Thomas
    Sep 29, 2013 at 11:01

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:


# Location of logfile

# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock

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 "---------------------------------"

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

        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."
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgConnection") || [ "$secDiff" -gt "$maxDiffSec" ]; then
                echo $(date)": "$msgConnection". "$msgDiff >> $LOGFILE
        # No NTP connection
        echo $msgNoConnection
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgNoConnection"); then
                echo $(date)": $msgNoConnection" >> $LOGFILE
  • you should really have this on github Aug 23, 2014 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, 2018 at 15:34

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, 2017 at 21:33

I would add auto create log file

# Location of logfile

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

# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock

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, 2014 at 18:31
  • thanks - useful addition. please consider using comments instead of adding another answer.
    – jitter
    Jun 3, 2017 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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