I have a working bash script and I want to run this script every minute with root privileges using crontab. I created my crontab as follows.

>sudo crontab -e

Then the file opened and I entered my cron job as follows.

* * * * * /home/pi/myscript.sh

This works. I see that my script is being run every minute. However, some of the lines of code in my script need to have root privileges to run. So, my script fails as a result even though it is being run every minutes. Then, I did what others suggested online.

* * * * * root /home/pi/myscript.sh

This didn't work at all. Cron failed to execute my script completely after adding root. Then, I tried the following by adding the user pi

* * * * * pi /home/pi/myscript.sh

Again, this didn't work at all. As before Cron failed to run my script. However, when I run the script right on terminal like sudo ./myscript.sh, it runs successfully with no problems. I understand that when you sudo crontab -e, it supposed to open cron jobs for root or superuser by default.

My script's permission is anyone can execute, anyone can read and only owner can modify.

My PI is PI2 and running Raspbian (Jessie).

  • Have you looked for similar questions? A script running from the terminal but not from a crontab is a common question caused by a common mistake and with a common answer.
    – joan
    Nov 18 '16 at 16:52
  • I have been looking for solution on stackoverflow and online. I have not come across anyone having this is issue or problem.
    – ThN
    Nov 18 '16 at 17:32
  • 2
    What about the related questions over to the top right ----->
    – joan
    Nov 18 '16 at 17:39

I made the following change and it works.

* * * * * sudo bash -x /home/pi/myscript.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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