0

I have a script I just created which runs fine under root. I added a cron job but it doesn't run right

Commands are:

gpio -g mode 17 input
getval="$(gpio read 17)"
todayis=$(date) 
getstatus=$getval 
if [ "$getstatus" = "0"  ]; then 
power="Power ON"
else
power="Power OFF"
echo $todayis $power $getstatus >> /var/www/turnmeon.log

Log file when run as root:

Wed Nov 8 21:11:41 UTC 2017 Power ON 0

Log file when run as crontab:

Wed Nov 8 21:11:41 UTC 2017 Power OFF

There is no 1 at the end like I might expect if OFF really was true.

Why would it run under a bash prompt but not as a crontab job

*/2 * * * * bash /myscript.sh
1

2 Answers 2

0

Sounds like it's a permission issue. You're probably editing the crontab as your desktop user, which means tasks will run under the desktop user. You need to edit the crontab as root if the task requires root

3
  • it's only a shell install - from root im running crontab -e Nov 8, 2017 at 21:42
  • are you sure? add whoami >> /var/www/turnmeon.log to the end of your script
    – Outurnate
    Nov 8, 2017 at 21:48
  • confirmed to be running as root - weird Nov 8, 2017 at 22:03
0

The likely cause is that gpio isn't in the PATH available in cron. Specify the full path to your gpio command, such as

getval="$(/sbin/gpio read 17)"

Find out where it is on your system using which gpio.

I got bitten by this before because I tinker with embedded solutions a lot. If the script is more complex, I usually initialize PATH and LD_LIBRARY_PATH near the start to make things easier.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .