Python Forum
Script ends, does not start again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script ends, does not start again
#1
I'm using the following script, wich is a timelapse script. It works, no problem. It starts on my raspberry 3 (raspian) by a cronjob on (re)boot.

Because i'm not intrested in photo of a dark night (say between 10 pm and 6 am) i changed this:
# Run a WHILE Loop of infinitely
while True:
   
   d = datetime.now()
   if d.hour < 99:
in this:

# Run a WHILE Loop of infinitely
while True:
   
   d = datetime.now()
if d.hour > 6 and d.hour < 22:
When it starts at say 3 pm, it runs no problem till 22 but then it does not start again the next morning at 6

total script:

#!/usr/bin/env python
#
#  raspiLapseCam.py
#
#  Created by James Moore on 28/07/2013.
#  Modified by James Moore on 13/11/2013.
#  Copyright (c) 2013 Fotosyn. All rights reserved.
#
#  Raspberry Pi is a trademark of the Raspberry Pi Foundation.

#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:

#  1. Redistributions of source code must retain the above copyright notice, this
#  list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright notice,
#  this list of conditions and the following disclaimer in the documentation
#  and/or other materials provided with the distribution.>

#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
#  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#  The views and conclusions contained in the software and documentation are those
#  of the authors and should not be interpreted as representing official policies,
#  either expressed or implied, of the FreeBSD Project.

# This script sets up and runs a Python Script which, at intervals invokes a capture 
# command to the Raspberry Pi camera, and stores those files locally in a dynamically
# named folder.

# To invoke, copy this script to an easy to find file location on your Raspberry Pi
# (eg. /home/pi/), log into your Raspberry Pi via terminal and type:
#
# sudo python /your/file/location/raspiLapseCam.py (add &) if you wish to run as a
# background task. A process ID will be shown which can be ended with

# sudo kill XXXX (XXXX = process number)

# Based on your settings the application will now begin capturing images
# saving them to your chosen file location (same as current location of this file as default.

# Import some frameworks
import os
import time
import RPi.GPIO as GPIO
import logging
from datetime import datetime

# Grab the current datetime which will be used to generate dynamic folder names
d = datetime.now()
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initHour = "%02d" % (d.hour)
initMins = "%02d" % (d.minute)

# Define the location where you wish to save files. Set to HOME as default. 
# If you run a local web server on Apache you could set this to /var/www/ to make them 
# accessible via web browser.
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) + str(initHour) + str(initMins)
os.mkdir(folderToSave)

# Set up a log file to store activities for any checks.
logging.basicConfig(filename=str(folderToSave) + ".log",level=logging.DEBUG)
logging.debug(" R A S P I L A P S E C A M -- Started Log for " + str(folderToSave))
logging.debug(" Support at ")

# Set the initial serial for saved images to 1
fileSerial = 1

# Run a WHILE Loop of infinitely
while True:
   
   d = datetime.now()
   if d.hour < 99:
       
       # Set FileSerialNumber to 000X using four digits
       fileSerialNumber = "%04d" % (fileSerial)
       
       # Capture the CURRENT time (not start time as set above) to insert into each capture image filename
       hour = "%02d" % (d.hour)
       mins = "%02d" % (d.minute)
       
       # Define the size of the image you wish to capture. 
       imgWidth = 800 # Max = 2592 
       imgHeight = 600 # Max = 1944
       print " ====================================== Saving file at " + hour + ":" + mins
       
       # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode
       # Change these settings where you see fit and to suit the conditions you are using the camera in
       os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg  -sh 40 -awb auto -mm average -v")

# Write out to log file
logging.debug(' Image saved: ' + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg")

       # Increment the fileSerial
       fileSerial += 1
       
       # Wait 60 seconds (1 minute) before next capture
       time.sleep(60)
       
   else:
       
       # Just trapping out the WHILE Statement
       print " ====================================== Doing nothing at this time"
Reply
#2
please, provide the actual code (i.e. the one with your changes).
Reply
#3
Couldn't edit anymore.
here the code as i use it

#!/usr/bin/env python
#
#  raspiLapseCam.py
#
#  Created by James Moore on 28/07/2013.
#  Modified by James Moore on 13/11/2013.
#  Copyright (c) 2013 Fotosyn. All rights reserved.
#
#  Raspberry Pi is a trademark of the Raspberry Pi Foundation.

#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:

#  1. Redistributions of source code must retain the above copyright notice, this
#  list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright notice,
#  this list of conditions and the following disclaimer in the documentation
#  and/or other materials provided with the distribution.>

#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
#  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#  The views and conclusions contained in the software and documentation are those
#  of the authors and should not be interpreted as representing official policies,
#  either expressed or implied, of the FreeBSD Project.

# This script sets up and runs a Python Script which, at intervals invokes a capture 
# command to the Raspberry Pi camera, and stores those files locally in a dynamically
# named folder.

# To invoke, copy this script to an easy to find file location on your Raspberry Pi
# (eg. /home/pi/), log into your Raspberry Pi via terminal and type:
#
# sudo python /your/file/location/raspiLapseCam.py (add &) if you wish to run as a
# background task. A process ID will be shown which can be ended with

# sudo kill XXXX (XXXX = process number)

# Based on your settings the application will now begin capturing images
# saving them to your chosen file location (same as current location of this file as default.

# Import some frameworks
import os
import time
import RPi.GPIO as GPIO
import logging
from datetime import datetime

# Grab the current datetime which will be used to generate dynamic folder names
d = datetime.now()
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initHour = "%02d" % (d.hour)
initMins = "%02d" % (d.minute)

# Define the location where you wish to save files. Set to HOME as default. 
# If you run a local web server on Apache you could set this to /var/www/ to make them 
# accessible via web browser.
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) + str(initHour) + str(initMins)
os.mkdir(folderToSave)

# Set up a log file to store activities for any checks.
logging.basicConfig(filename=str(folderToSave) + ".log",level=logging.DEBUG)
logging.debug(" R A S P I L A P S E C A M -- Started Log for " + str(folderToSave))
logging.debug(" Support at ")

# Set the initial serial for saved images to 1
fileSerial = 1

# Run a WHILE Loop of infinitely
while True:
   
  d = datetime.now()
 if d.hour > 6 and d.hour < 22:
       
      # Set FileSerialNumber to 000X using four digits
      fileSerialNumber = "%04d" % (fileSerial)
       
      # Capture the CURRENT time (not start time as set above) to insert into each capture image filename
      hour = "%02d" % (d.hour)
      mins = "%02d" % (d.minute)
       
      # Define the size of the image you wish to capture. 
      imgWidth = 800 # Max = 2592 
      imgHeight = 600 # Max = 1944
      print " ====================================== Saving file at " + hour + ":" + mins
       
      # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode
      # Change these settings where you see fit and to suit the conditions you are using the camera in
      os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg  -sh 40 -awb auto -mm average -v")

# Write out to log file
logging.debug(' Image saved: ' + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg")

      # Increment the fileSerial
      fileSerial += 1
       
      # Wait 60 seconds (1 minute) before next capture
      time.sleep(60)
       
  else:
       
      # Just trapping out the WHILE Statement
      print " ====================================== Doing nothing at this time"
Reply
#4
The indentation within the while loop is a mess. Here is what I think it should be


#!/usr/bin/env python
#
#  raspiLapseCam.py
#
#  Created by James Moore on 28/07/2013.
#  Modified by James Moore on 13/11/2013.
#  Copyright (c) 2013 Fotosyn. All rights reserved.
#
#  Raspberry Pi is a trademark of the Raspberry Pi Foundation.

#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:

#  1. Redistributions of source code must retain the above copyright notice, this
#  list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright notice,
#  this list of conditions and the following disclaimer in the documentation
#  and/or other materials provided with the distribution.>

#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
#  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#  The views and conclusions contained in the software and documentation are those
#  of the authors and should not be interpreted as representing official policies,
#  either expressed or implied, of the FreeBSD Project.

# This script sets up and runs a Python Script which, at intervals invokes a capture 
# command to the Raspberry Pi camera, and stores those files locally in a dynamically
# named folder.

# To invoke, copy this script to an easy to find file location on your Raspberry Pi
# (eg. /home/pi/), log into your Raspberry Pi via terminal and type:
#
# sudo python /your/file/location/raspiLapseCam.py (add &) if you wish to run as a
# background task. A process ID will be shown which can be ended with

# sudo kill XXXX (XXXX = process number)

# Based on your settings the application will now begin capturing images
# saving them to your chosen file location (same as current location of this file as default.

# Import some frameworks
import os
import time
import RPi.GPIO as GPIO
import logging
from datetime import datetime

# Grab the current datetime which will be used to generate dynamic folder names
d = datetime.now()
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initHour = "%02d" % (d.hour)
initMins = "%02d" % (d.minute)

# Define the location where you wish to save files. Set to HOME as default. 
# If you run a local web server on Apache you could set this to /var/www/ to make them 
# accessible via web browser.
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) + str(initHour) + str(initMins)
os.mkdir(folderToSave)

# Set up a log file to store activities for any checks.
logging.basicConfig(filename=str(folderToSave) + ".log",level=logging.DEBUG)
logging.debug(" R A S P I L A P S E C A M -- Started Log for " + str(folderToSave))
logging.debug(" Support at ")

# Set the initial serial for saved images to 1
fileSerial = 1

# Run a WHILE Loop of infinitely
while True:
   d = datetime.now()
   if d.hour > 6 and d.hour < 22:
       # Set FileSerialNumber to 000X using four digits
       fileSerialNumber = "%04d" % (fileSerial)
           
       # Capture the CURRENT time (not start time as set above) to insert into each capture image filename
       hour = "%02d" % (d.hour)
       mins = "%02d" % (d.minute)
           
       # Define the size of the image you wish to capture. 
       imgWidth = 800 # Max = 2592 
       imgHeight = 600 # Max = 1944
       print " ====================================== Saving file at " + hour + ":" + mins
           
       # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode
       # Change these settings where you see fit and to suit the conditions you are using the camera in
       os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg  -sh 40 -awb auto -mm average -v")
    
       # Write out to log file
       logging.debug(' Image saved: ' + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg")
    
       # Increment the fileSerial
       fileSerial += 1
          
       # Wait 60 seconds (1 minute) before next capture
       time.sleep(60)
   else:
       
       # Just trapping out the WHILE Statement
       print " ====================================== Doing nothing at this time"
Reply
#5
Are you sure that there is no still working process at 6 am before starting it again? As I see, your program doesn't end. It runs an endless loop. You have to put sys.exit(0) inside the else block.

import sys

while True:
    d = datetime.now()

    if d.hour > 6 and d.hour < 22:
    # do your time lapse things

    else:
        
        # Just trapping out the WHILE Statement
        print " ====================================== Doing nothing at this time"
        sys.exit(o)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Mar-30-2017, 12:06 PM)wavic Wrote: Are you sure that there is no still working process at 6 am before starting it again? As I see, your program doesn't end. It runs an endless loop. You have to put sys.exit(0) inside the else block.

import sys

while True:
    d = datetime.now()

    if d.hour > 6 and d.hour < 22:
    # do your time lapse things

    else:
        
        # Just trapping out the WHILE Statement
        print " ====================================== Doing nothing at this time"
        sys.exit(o)

Ah, maybe i did not explianit well enough, english is not my maiden language. The script does not have to stop, as i see it, it should only make a photo between 6am and 10 pm, it does 'end' taking pictures at 10 pm, but it does not 'start' again taking pictures a 6am
Reply
#7
Quote:
   else:
        
       # Just trapping out the WHILE Statement
       print " ====================================== Doing nothing at this time"

You should probably put time.sleep() there, with pretty much any increment (even just 1 second). As it stands now, it'll run the loop continuously, non-stop, while the sun's down. Which means the device could get very hot. Depending on how hot is it outside, it could get hot enough to cause permanent damage.
Reply
#8
I don't know how much harmful is this for the hardware but it's not needed. Use cron to run the script, use datetime.datetime.now() to stop it at 22:00. Cron is just for that.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Or, just have cron run it every minute, on the hours you care about, and get rid of the while loop and time checks and sleep() completely.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pywin32: Outlook connection ends with 'operation aborted' on one machine tstone 0 2,372 May-03-2022, 04:29 AM
Last Post: tstone
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,518 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Regex: a string does not starts and ends with the same character Melcu54 5 2,402 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  threadlocals are garbage collected before thread ends akv1597 0 1,792 Mar-09-2021, 12:13 PM
Last Post: akv1597
  Running a few lines of code as soon as my timer ends nethatar 3 2,396 Feb-26-2021, 01:02 PM
Last Post: jefsummers
  Writing to file ends incorrectly project_science 4 2,679 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Keep Application running after Python script ends PEGylated_User 0 1,968 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  Looking for help on making a script [no idea where to start] Chietnemese 1 1,745 Jun-26-2020, 03:50 AM
Last Post: Larz60+
  how to stop and start a script for 30 seconds laspaul 9 7,630 Jan-16-2020, 02:13 PM
Last Post: laspaul
  Multiple start of script dev1755 2 2,160 Sep-22-2019, 10:44 PM
Last Post: dev1755

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020