5

I have a python3 script that uses OpenCV and a Raspberry Pi cam. It runs almost 24/7. Using crontab the pi will reboot every night at midnight and the script will begin running again. This is mostly so that if something goes wrong (like if one of the python scripts unexpectedly stops and fails to restart somehow) everything has a chance to be reset, and because I'm afraid running the camera 24/7 is somehow bad for it.

To prevent potential problems I was planning to killall python before the reboot, and possibly release the camera as well. The problem is, I don't know if it is even necessary to kill python, release the camera, or reboot. I'm just assuming it's a good idea. If releasing the camera is necessary then I don't know how I'd do that either.

Can someone help me out here?

This is what I picture the crontab would look like (I haven't tested it yet and I'm new to this, so I apologize if my syntax is wrong):

@reboot sudo modprobe bcm2835-v4l2
@reboot python3 /home/pi/motion/motionDetection.py
@reboot python3 /home/pi/motion/CPUtracker.py
*/5 * * * * pgrep -f /home/pi/motion/motionDetection.py || nohup python3 /home/pi/motion/motionDetection.py
*/5 * * * * pgrep -f /home/pi/motion/CPUtracker.py || nohup python3 /home/pi/motion/CPUtracker.py
59 23 * * * killall python
###59 23 * * * [code to release camera]
0 0 * * * sudo reboot
StarSweeper
  • 455
  • 3
  • 12

3 Answers3

6

There is NO NEED to do anything before a reboot.

If you issue a sudo reboot command Linux will manage an orderly shutdown of the software.

This is, however, a poor way of ensuring a reliable 24/7 system. If it crashes there is no guarantee the reboot itself would happen.

The normal solution would be to use a watchdog timer. The Pi has an inbuilt hardware watchdog which is suitable for the purpose - although setting it up can be a bit tricky. It SHOULD cause a restart on system lockup, but it would be better to have it monitor your process.

As a side note your crontab probably wouldn't work - you should specify FULL PATHS to all executables, including python3.

Milliways
  • 62,573
  • 32
  • 113
  • 225
3

No, it is not necessary to stop the Pi camera before rebooting.

joan
  • 71,852
  • 5
  • 76
  • 108
3

No there is no need to stop the camera before reboot.

Also preforming killall python is not doing you any good as the reboot is going to force the program to close anyway. If you are worried about your program needing to shutdown gracefully, you should notify the program to shutdown rather then kill it. A simple way would be to use touch in your crontab like so:

touch /path/to/some/file

and then have your python code look to see if that file exists

import os.path
if os.path.isfile("/path/to/some/file"):
    #do any other shutdown tasks, close files ect....
    os.remove("/path/to/some/file")
    #to make sure its not there when the script comes back up 
    #it will be created again on the next crontab cycle
    exit()

Now obviously this is a very insecure way to do it, as anyone could create this file to kill your program, but I am guessing this is just for hobby use and there will be no issue there.

Chad G
  • 1,063
  • 1
  • 7
  • 14