1

Bash script in How can I automatically update the hwclock with NTP when I have internet connection seems not to run as expected in Raspbian Stretch 9.4

The log file will be created with the first reading but no more readings will be appended to the file.

pi@raspberrypi:~$ ls -al hwc*
-rw-r--r-- 1 pi pi   84 Jul 31 18:40 hwcsynch.log
-rwxr-xr-x 1 pi pi 1783 Jul 31 18:39 hwcsynch.sh

crontab -e to have the script run every minute for testing purposes:

* * * * * /home/pi/hwcsynch.sh

Only the first reading is added to the file. Following readings will not.

pi@raspberrypi:~$ cat hwcsynch.log
Tue 31 Jul 18:40:02 CEST 2018: Connection to time-server. HwClock difference: 1 sec

And then run the bash script from prompt just to make sure it works:

pi@raspberrypi:~$ ./hwcsynch.sh
Connection to time-server
---------------------------------
HwClock: 1533055729 sec
SysClock: 1533055728 sec
---------------------------------
Difference: 1 sec

So I assume the script is running properly. Maybe it has to do with...?

if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgConnection") || [ "$secDiff" -gt "$maxDiffSec" ]; then
        echo $(date)": "$msgConnection". "$msgDiff >> $LOGFILE
fi
  • 3
    Do NOTHING and it should work. – Milliways Jul 31 '18 at 22:24
  • what and where are $msgConnection, $secDiff, $maxDiffSec, $msgDiff and $LOGFILE defined? – Jaromanda X Jul 31 '18 at 23:33
  • try * * * * * /home/pi/hwcsynch.sh > /tmp/last_hwsync in the crontab. It will give you the stdout of the script which will probably give a better understanding of what's going on. – Ljm Dullaart Aug 1 '18 at 14:07
0

@LjmDullaart I have done it as you suggest:

pi@raspberrypi:~$ cat /tmp/last_hwsynch  
Connection to time-server  
---------------------------------  
HwClock: 1533155224 sec  
SysClock: 1533155223 sec  
---------------------------------  
Difference: 1 sec  

Every time I can that fie I notice time moves forward, so yes, the script works. I have achieved the same goal by ./hwcsynch.sh

After cat'ing the script log now I notice:

pi@raspberrypi:~$ cat hwcsynch.log
Tue 31 Jul 18:40:02 CEST 2018: Connection to time-server. HwClock difference: 1 sec
Tue 31 Jul 19:08:01 CEST 2018: No connection to time-server
Tue 31 Jul 19:15:04 CEST 2018: Connection to time-server. HwClock difference: 1 sec
Tue 31 Jul 21:04:01 CEST 2018: No connection to time-server
Tue 31 Jul 23:37:03 CEST 2018: Connection to time-server. HwClock difference: 1 sec
Wed 1 Aug 00:57:01 CEST 2018: No connection to time-server
Wed 1 Aug 05:56:04 CEST 2018: Connection to time-server. HwClock difference: 1 sec
Wed 1 Aug 08:40:01 CEST 2018: No connection to time-server
Wed 1 Aug 17:07:02 CEST 2018: Connection to time-server. HwClock difference: 1 sec
Wed 1 Aug 19:32:01 CEST 2018: No connection to time-server
Wed 1 Aug 19:38:02 CEST 2018: Connection to time-server. HwClock difference: 1 sec

Strange... at 05:56:04 yes I switched on the router, the Pi went online and connected to time-server and so it is reflected in the log. However, the next time mark in the next line is at 08:40:01 stating no connection. Weird as I switched off the router at about 06:30:00 so just about that time the log should have a line reading no connection when in fact the log shows that has taken place at 08:40:01. So something's not right.

@JaromandaX those are defined in the bash script by Thomas (link in first message):

msgConnection="Connection to time-server"
secDiff=$(($secHwClock-$secSysClock))
# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock
maxDiffSec="2"
msgDiff="HwClock difference: $secDiff sec"
LOGFILE="/home/pi/hwcsynch.log"
0

As I stated in the comment: put

* * * * * /home/pi/hwcsynch.sh > /tmp/last_hwsync

in the crontab. The result was:

$ cat /tmp/last_hwsynch 
Connection to time-server 
--------------------------------- 
HwClock: 1533155224 sec 
SysClock: 1533155223 sec 
--------------------------------- 
Difference: 1 sec

Looking at the script that is referenced:

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"

This means that the clock is only updated if the difference > $maxDiffSec, which is set to 2 earlier in the script. So no further action will be taken.

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.