GPIO command for raspberry not working via crontab

此生再无相见时 提交于 2021-01-27 17:35:38

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14308147/gpio-command-for-raspberry-not-working-via-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!