2

I've got a Raspberry Pi. I've installed on it WiringPi for pilot GPIO on the board via command line.

I've done a script called aggiornaora.sh

gpio -g write 18 1 #it set the GPIO port to 1
#log with reverse append
(echo 'accensione';date;echo ' ') | cat - logstufa.txt > temp && mv temp logstufa.txt

This script work fine if I try to exec it directly via sh aggiornaora.sh. But when cron run the script, it do only the second action. All with root permission. I've checked this problem through "gpio readall".

In your opinion what could be the problem?

2 Answers 2

7

Note that when you execute something from crontab, it will not necessarily have the same environment variables set.

Most important env. variable for you is PATH. I think that under cron your PATH does not contain directory in which your gpio command is located.

You can add line like this as first line of your crontab:

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

and this should fix your issue, if gpio is located in one of directories listed above.

Typical way to debug crontab issues is to make sure that env. variables are what you expect them to be. Add line like this into first line of the script to debug:

set > /path/to/log/file    # be sure that this file is writable

Run it once manually, copy logfile aside. Then, run it again from cron, compare output - you will see what makes them different.

3
  • Thanks a lot mvp for your quick and precise reply! I've added the path line and it works nicely!! now i would try to debug and compare! thanks!!
    – malgaboy
    Commented Jan 13, 2013 at 21:33
  • Glad it worked for you! Note that it is encouraged here to accept and/or upvote answers that helped you solve your problem. And by accepting you will increase your own reputation.
    – mvp
    Commented Jan 13, 2013 at 21:37
  • oh nice! but it says me: vote up requires 15 reputations. When i'll reach 15 i will upvote you ;)
    – malgaboy
    Commented Jan 13, 2013 at 21:43
1

Yes, I have had the same problem (using gpio commands in crontab). A quick fix that worked for me was to prepend an 'sudo' to the gpio commands like: sudo gpio write 0 1

When I went though my bash script and prepenneded the 'sudo's to all of my gpio commands everything works fine.

The previous answer suggested by mvp is the 'better' way to fix these types of problems with crontab. Hope this helps

1
  • it might make more sense to run your crontab entry as root rather than the normal user; ie, instead of crontab -e use sudo crontab -e to add you entry.
    – ccpizza
    Commented Oct 6, 2019 at 4:55

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.