0

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).

4
  • 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, 2016 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, 2016 at 17:32
  • 2
    What about the related questions over to the top right ----->
    – joan
    Nov 18, 2016 at 17:39
  • I think your answer is wrong (see my comment below). If you'll edit your question, and include the contents of myscript.h, perhaps we can help?
    – Seamus
    Jan 26 at 14:01

1 Answer 1

-1

I made the following change and it works.

* * * * * sudo bash -x /home/pi/myscript.sh
1
  • When you run a script from the root crontab - as you have with sudo crontab -e, the script runs with root privileges.That means all of the commands in the script run with root privileges. You say, some of the lines of code in my script need to have root privileges to run. So, my script fails.... I think you have made another mistake; perhaps failed to recognize that cron's default shell is sh - not bash??.
    – Seamus
    Jan 26 at 13:56

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.