3

I am trying to find a way of leaving my python program running continuously, but ensuring that the main script is only 'active' during nightime hours (e.g. between 21:30 and 04:30 for example)

Background: My program records motion activated video & activates a floodlight.

The main program segment is below:

...I am currently running this each evening and then killing in the morning with Ctrl-C..!

try:

signal.signal(signal.SIGHUP,SigHandler) # proper exit for SIGHUP (terminal hangups)

while GPIO.input(PIR_PIN)==1: # Loop until PIR output is stabilised at 0
    pass

GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MotionDetected)

signal.pause() #pauses main program forever & waits for PIR Rising Edge unless interrupted

except KeyboardInterrupt:
    print("")

finally:
    GPIO.output(16, GPIO.LOW) #floodlight off
    GPIO.cleanup()

Is there a way of checking time periodically in a parallel thread perhaps, and suspending program if time betweeen certain hours?

Might the solution posted below work for my program perhaps if I add the thread statement before my try statement?...

How to schedule python script to exit at given time

CodeFlan
  • 105
  • 1
  • 8
  • 1
    Normally it should be handled on OS level. – Olvin Roght Aug 20 '20 at 21:04
  • time module with time.sleep() is your needed tool, here a full tuto https://www.journaldev.com/15797/python-time-sleep –  Aug 20 '20 at 21:13
  • 1
    @Cyber-Tech that won't work because the main thread pauses and waits for an event. It can't simultaneously check how much time has elapsed while waiting for input. – jarcobi889 Aug 20 '20 at 21:16

2 Answers2

2

First, create a cron job (I believe you're on Linux because of the GPIO calls), to start the script every night at 21:30.

30 21 * * * python /path/to/python/script

Then, create a second cron job at 4:30 to kill any existing processes that are running your program.

30 4 * * * pkill -f /path/to/python/script # Will need to be edited to the actual process name that runs.

Killing a process by matching name

jarcobi889
  • 815
  • 5
  • 16
  • Thanks @jarcobi889. Yes Linux OS. I'll explore the cron approach further, however the problem / challenge here is getting the program to exit gracefully with the kill command. (I.e. turning off the floodlight, finish recording a video if already in progress, and cleanup the GPIO states before exit) Any ideas on this approach? – CodeFlan Aug 27 '20 at 11:06
  • 1
    You would use the [signal](https://docs.python.org/3.8/library/signal.html) module to detect the kill interrupt and use that to set your final states like finishing recording video and turning off the floodlight before exiting. – jarcobi889 Aug 27 '20 at 19:18
  • Thanks - will further explore that module and post final code here if / when successful :-) – CodeFlan Aug 28 '20 at 08:03
1

Python Pause https://github.com/jgillick/python-pause/ supports pause until a certain datetime.

Also Java Threads 2nd edition has an example code of a job scheduler; maybe you can use it as a blue print.

Mat90
  • 169
  • 1
  • 9
  • Thanks for the idea. I've paused this project for many months but hope to get back to it in future and will try this out. – CodeFlan Sep 08 '22 at 14:02