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

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?

  • Your question seems to be formatted improperly. Please use code fences when necessary. I've added some to show you how - please review & modify your question as required.
    – Seamus
    Jan 21, 2021 at 21:41
  • "I added a redirect ..." What redirect? Where is it - how did you do this? And please make it clear: Do you need sudo to run the job successfully from the command line, or not?
    – Seamus
    Jan 21, 2021 at 21:47

1 Answer 1


Your question is currently unclear on some points & I've requested clarification in the comments. Until then, this may help:

cron doesn't have the same PATH that you have in the interactive shell (bash), and the system cannot find the executable file(s) you've specified in your crontab. There are a couple of ways to solve this, but perhaps the cleanest is to simply specify the full path in your crontab:

0 */2 * * * /usr/sbin/service motion restart

I have no idea what motion is, or where it is located. You may need to add its full path also.

You stated, "When I run the script as root from the command line, it indeed restarts the motion service.".

If root privileges are required to run this program, then you will need to run it under the root crontab instead of your crontab:

$ sudo crontab -e

Once the root crontab opens in the editor, make your crontab entries without using sudo; i.e.:

0 */2 * * * /usr/sbin/service motion restart

Some other items:

  1. The script you created will have the same issues as your initial crontab entries: You should specify the script's full path in crontab AND you must specify the full path of your executables in the script.

  2. logger may not be capturing the most important information: stderr. Use a redirect instead to capture stderr and stdout to a file:

    0 */2 * * * /usr/sbin/service motion restart >> /home/pi/mycronlog.txt 2>&1
  3. As a "learning experiment", try adding the following to your crontab:

    0 */2 * * * /usr/bin/logger <<< echo "This is your PATH in cron: $PATH"
  • You are exactly correct. I needed the path. Thanks. Jan 21, 2021 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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