betwixt
Posts: 3
Joined: Mon Jan 01, 2018 9:52 am

time error in a script

Mon Jan 01, 2018 10:06 am

This one has me mystified, all I can think of is the relatively slow speed of the Pi B is causing some kind of race condition. It's a simple script that is supposed to run and annotate a picture from the Pi's camera and upload it to a web site but only between certain times. It works fine except that occasionally it runs when it shouldn't. Some days it takes a single picture at 00:00 and uploads it when it should only work after 7am and before 9pm.
The script (with log in information obscured):

Code: Select all

#!/bin/bash
## outer loop runs forever
while true;
do
   ## inner loop restricts operation to after 07:00 and before 21:00
   while [ $(date +"%k") -gt 7 -a $(date +"%k") -lt 21 ]; 

   do
      echo taking picture
      raspistill -q 20 -t 500 -n -o /home/pi/webcam/raw_camera.jpg -w 320 -h 240
      nowday=$(date +"%A,")
      nowdate=$(date +"%d-%b-%Y")
      nowtime=$(date +"%H:%M")

      echo annotating footer image
      convert  /home/pi/webcam/banner_320x30.jpg \
               -fill '#0008' -draw 'roundrectangle 5,5,315,25,20,20' \
               -fill white   -annotate +20+20 '© dyfi.com' \
               -annotate +95+20 "$nowday"  \
               -annotate +165+20 "$nowdate" \
               -annotate +270+20 "$nowtime" \
               /home/pi/webcam/footer_320x30.jpg

      echo combining camera and footer images
      convert -append /home/pi/webcam/raw_camera.jpg /home/pi/webcam/footer_320x30.jpg /home/pi/webcam/webcam_image.jpg

      echo uploading image
      ncftpput -u xxxxxxxxx -p xxxxxxxxxxxxxxxx xxxxxxxxxx.com /xxxxxxxxx/xxxxxxxxx /home/pi/webcam/webcam_image.jpg
      echo waiting 30 seconds
      sleep 30
   done
done
exit 0
Anyone any ideas why the extra picture is produced? It is always at 00:00, as though it somehow slips through the time check.

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: time error in a script

Mon Jan 01, 2018 1:20 pm

You read the hour twice, since your outer while loop is running very quickly when outside the active block so time could easily switch from 23:59:59 to 00:00:00 between the two tests of the current hour. Right before midnight hour = 23 so the first test is true (23 > 7), then time advances to midnight before you do the second test so that is now true also (0 < 21).
She who travels light — forgot something.

betwixt
Posts: 3
Joined: Mon Jan 01, 2018 9:52 am

Re: time error in a script

Mon Jan 01, 2018 9:43 pm

I see what you are saying and I agree it could cause a picture to be uploaded outside the permitted time but surely it could only happen if the inner loop was already executing, in other words immediately after 21:00.

This is a slightly different scenario, it actually executes the inner loop once at 00:00, including taking the picture, annotating it and uploading it. You can see it at http://www.dyfi.com/Dyficam.html. The last picture taken in the evening is at 20:59 or 21:00 depending on whether the inner loop is already executing but sometimes, a new image appears at midnight with 00:00 as the time stamp. It gets overwritten at 08:00 as it starts up normally again.

User avatar
davidcoton
Posts: 5083
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: time error in a script

Mon Jan 01, 2018 10:45 pm

betwixt wrote:
Mon Jan 01, 2018 9:43 pm
I see what you are saying and I agree it could cause a picture to be uploaded outside the permitted time but surely it could only happen if the inner loop was already executing, in other words immediately after 21:00.

This is a slightly different scenario, it actually executes the inner loop once at 00:00, including taking the picture, annotating it and uploading it. You can see it at http://www.dyfi.com/Dyficam.html. The last picture taken in the evening is at 20:59 or 21:00 depending on whether the inner loop is already executing but sometimes, a new image appears at midnight with 00:00 as the time stamp. It gets overwritten at 08:00 as it starts up normally again.
No, the test is in the outer loop. When it passes, the inner loop runs, regardless of what it did last time. Otherwise it wouldn't restart at 07:00.
Try reversing the two tests, so it won't matter if the midnight occurs between them:

Code: Select all

   while [ $(date +"%k") -lt 21 -a $(date +"%k") -gt 7 ]; 
Signature retired

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: time error in a script

Mon Jan 01, 2018 11:55 pm

The only thing in your outer loop is the inner loop. Whilst the inner while condition is false then the outer loop immediately goes back to trying to start the inner loop.

As davidcoton says, reversing the tests fixes the problem as the hour rolling over between the two tests doesn't affect it that way, though I'd probably do it by using the same hour value in both tests of the condition. Plus have a sleep in the outer loop to avoid having the process maxing the cpu due to constantly checking for the start.

Code: Select all

while true;
do
    hour=$(date +"%k")
    while [ $hour -gt 7 -a $hour -lt 21 ];
    do
        # Do your stuff
        hour=$(date +"%k")  # Get the new current hour
    done
    sleep 60 # give it at least a minute before checking again otherwise you'll have the cpu maxed out between 21:00:00 and 7:59:59
done
She who travels light — forgot something.

betwixt
Posts: 3
Joined: Mon Jan 01, 2018 9:52 am

Re: time error in a script

Tue Jan 02, 2018 10:52 am

Thanks guys!

I understand the problem now, I mistakenly looked at the date reading in the inner and outer loops when you did in fact state it was a consequence of the clock advancing between the two checks on the same line. I'll edit as suggested by both of you, read the hour once and use it twice and also move the 'sleep' to where it hogs less time.

The trouble with this Pi is it is in a most inaccessible place so I'll have to rely on SSH / SCP to control it and upload the new script. For some reason, despite using the latest 'Stretch' OS, the VNC doesn't start up automatically or manually on this system.

Return to “General programming discussion”