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
  • 1
    "I'm afraid running the camera 24/7 is somehow bad for it" <-- this is highly unlikely to be true. – R.. May 2 '18 at 21:10
  • 1
    I don't like this method of handling process supervision. If you have a systemd-based distro, I strongly recommend making a service which has Type=simple and Restart=always defined. Then you don't have to reboot anything, and you don't have to restart the script if it crashes. Also consider writing a script which monitors the camera script and ensures it is producing sane data, and if not, notifying you so you can debug it. – Yet Another User May 3 '18 at 1:55
  • @YetAnotherUser Thanks for the advice. I'm really new to this. What I wrote in the post didn't work, but the service thing you talked about has in the past so I was going to switch back to that tomorrow. – StarSweeper May 3 '18 at 5:50
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.

  • Yeah the crontab didn't work. I'm making some changes to it tomorrow. I'll also Google for more information about watchdog. – StarSweeper May 3 '18 at 5:55
3

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

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.

  • 2
    This all being said, linux can run for a long, long time without needing to reboot, really should be able to just run a cron job that acts as a watchdog to ensure your scripts are running and restarts them if needed. – Chad G May 2 '18 at 22:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.