GPIO command for raspberry not working via crontab

Tag: bash , gpio Author: yangmin89 Date: 2012-12-28

first of all sorry for my bad English. 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? Thanks

Best Answer

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.

comments:

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!!
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.
oh nice! but it says me: vote up requires 15 reputations. When i'll reach 15 i will upvote you ;)