0

I have a shell script for getting data from some servers, in ~/.bin/shellScript :

OLD="$(curl --silent http://someServer:12345/stats.json | json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | json someKey")

echo "OLD $OLD - NEW $NEW"

I want to echo the results for running it interactively, but I've been wanting to log the results collected too. So crontab -e, and add */5 * * * * /home/user/.bin/shellScript >> /media/dump/scriptDump.

Running the script interactively works fine - I get OLD 123 - NEW 456, but when I look at what's been running from cron, I get OLD - NEW with no data being grabbed with curl.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Squidly
  • 2,707
  • 19
  • 43
  • 1
    What if you add the full path of `json` on `json someKey`? Crontab doe s work in a limited environment and some paths can be missing, so you need to give it the full of it. – fedorqui Dec 20 '13 at 10:13
  • It never occurred to me that I needed to fix $PATH in the shellscript (even though I knew that cron had a limited env'). /facepalm – Squidly Dec 20 '13 at 10:15
  • Hehe it always happens to all of us, no problem. So does it mean that now it is working to you? – fedorqui Dec 20 '13 at 10:21
  • 1
    Yeah. If you put that as an answer, I'll accept it. Cheers – Squidly Dec 20 '13 at 10:23

1 Answers1

1

As discovered in the comments, you need to add the full path of json when you are calling it. This is because crontab's limited environment.

So instead of

OLD="$(curl --silent http://someServer:12345/stats.json | json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | json someKey")

it has to be

OLD="$(curl --silent http://someServer:12345/stats.json  | /path/to/json someKey)"
NEW="$(curl --silent http://otherServer:12345/stats.json | /path/to/json someKey)"
                                                           ^^^^^^^^^^^^^

[[Note your second line had ") instead of )"]]


Otherwise, you can also add the json path into crontab, as indicated on How to get CRON to call in the correct paths:

PATH=/usr/local/sbin: ... :/path/of/json
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598