I have a raspberry pi that is running an outdoor camera. Occasionally, the camera 'locks' and I have to restart the service as root and it starts working again. Since the camera is outside it's in a sealed case and I'm try all other options before I decide I need to remove and rebuild.
So I tried to set up a cron job to restart the service every 2 hours.
First I tried restarting the service directly in cron
In crontab I had:
0 */2 * * * service motion restart
But it never restarted.
So to make sure cron was working, I changed my crontab to:
0 */2 * * * service motion restart
0 */2 * * * logger motion should have restarted
I checked the /var/log/messages
sometime later and sure enough, multiple messages saying 'motion should have restarted' but no logs showing that it ACTUALLY restarted. And indeed it had not when I checked the motion.log file and it was still locked up.
So I created a script called /home/pi/restart_camera
#!/bin/bash
service motion restart
logger script ran to restart camera
and then changed cron
to say
0 */2 * * * /home/pi/restart_camera
0 */2 * * * logger motion should have restarted
But when I checked sometime later, the camera was in a locked state, but /var/log/messages had multiple messages saying 'script ran to restart camera' and 'motion should have restarted' but still no logs showing that it ACTUALLY restarted. And again it had not when I checked the motion.log file and it was still locked up.
When I run the script as root from the command line, it indeed restarts the motion service.
I added a redirect and the error it gave is the following:
/home/pi/restart_camera: line 2: service: command not found
Any ideas here?
code fences
when necessary. I've added some to show you how - please review & modify your question as required.I added a redirect ...
" What redirect? Where is it - how did you do this? And please make it clear: Do you needsudo
to run the job successfully from the command line, or not?