【问题标题】:Why is curl not producing output when run in a shell script from cron?为什么 curl 在 cron 的 shell 脚本中运行时不产生输出?
【发布时间】:2013-12-20 10:11:55
【问题描述】:

我有一个用于从某些服务器获取数据的 shell 脚本,位于 ~/.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"

我想回显以交互方式运行它的结果,但我也一直想记录收集到的结果。 所以crontab -e,然后添加*/5 * * * * /home/user/.bin/shellScript >> /media/dump/scriptDump

以交互方式运行脚本可以正常工作 - 我得到 OLD 123 - NEW 456,但是当我查看从 cron 运行的内容时,我得到 OLD - NEW 没有被 curl 抓取的数据。

【问题讨论】:

  • 如果在json someKey上加上json的完整路径呢? Crontab 确实在有限的环境中工作,并且可能会丢失某些路径,因此您需要完整地提供它。
  • 我从来没有想过我需要在 shellscript 中修复 $PATH(即使我知道 cron 有一个有限的 env')。 /掌脸
  • 呵呵,我们每个人都会遇到这种情况,没问题。那么这是否意味着它现在对你有用?
  • 是的。如果你把它作为答案,我会接受它。干杯

标签: shell curl cron crontab


【解决方案1】:

在 cmets 中发现,您需要在调用时添加 json 的完整路径。这是因为 crontab 的环境有限。

所以不是

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

应该是的

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

[[注意你的第二行是")而不是)"]]


否则,您也可以将json路径添加到crontab中,如How to get CRON to call in the correct paths所示:

PATH=/usr/local/sbin: ... :/path/of/json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 2015-07-26
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2016-03-19
    相关资源
    最近更新 更多