I have a Raspberry Pi that runs a Python 3 program every night at 10pm to take a picture of my Solar Panel Meter, from a command in crontab.
This works fine but recently after years of running without problem the picture was just a black image. I traced this to the fact that although the camera was working the IR lights controlled by GPIO 7 were not working. I further traced this to a problem with the GPIO output which seems to fail regularly using lasting around a day before failing.
However, I found that by rebooting the Pi using the Menu option, it would work perfectly again for a while before failing again.
I would like therefore to reboot the Pi automatically at say 9pm each day to ensure it is working properly to take the picture.
Despite trying many ways to achieve automatic bootup using crontab, I cannot get it to work. The latest way I tried was:
0 21 * * * /sbin/shutdown -r now
previously:
0 21 * * * reboot
Could someone please advise how to use cron to reboot a Pi
Thank you
That looks correct to me as long as it is in root's crontab (sudo crontab -e not crontab -e). If it's in a normal user's crontab it'll likely be throwing an error that says you need to be root or that you need to authenticate but on a default setup that error will be discarded.
However as your aim is to "reboot the Pi automatically at say 9pm each day to ensure it is working properly to take the picture." this solution won't work in all cases. Both the cron daemon and the kernel need to be still running in order for the reboot to happen.
You might want to investigate the hardware watchdog. Once enabled and configured if it doesn't get poked by the kernel every 15s or so it does a hard reboot of the Pi. If the kernel has crashed/paniced it won't get poked and your Pi gets rebooted.
Statistics: Posted by thagrol — Tue Mar 05, 2024 4:58 pm