首页 文章

Shell脚本从CLI运行,但不从Crontab运行

提问于
浏览
2

我有一个shell脚本,它遍历目录中的每个JSon文件,并使用phantomJS创建一个highchart png .

调度cron任务以运行此脚本时出现问题 - (最初我使用了inotifywait但得到了相同的错误) .

shell脚本如下所示:

#!/bin/sh
for i in *.json; do
    filename="${i%.*}"
    phantomjs /var/www/highcharts.com/exporting-server/phantomjs/highcharts-convert.js -infile $i -outfile img/$filename.png -scale 2.5 -width 300 -constr Chart -callback /var/www/highcharts.com/exporting-server/phantomjs/callback.js
done

并且cron任务看起来像这样:

* * * * * /var/www/highcharts.com/exporting-server/phantomjs/test/createGraphs.sh >> /var/www/highcharts.com/exporting-server/phantomjs/highcharts.log

在日志文件中我收到错误:

“无法打开文件'* .json'”

从命令行运行时,shell脚本运行正常,但在尝试计划时会出现问题 .

1 回答

  • 3

    Cron在您的主目录中运行您的命令 . 我假设json文件不在您的主目录中,因此您的脚本失败并显示该错误 .

    将您的cron作业更改为cd到目录:

    * * * * * cd /path/to/json && /var/www/highcharts.com/exporting-server/phantomjs/test/createGraphs.sh >> /var/www/highcharts.com/exporting-server/phantomjs/highcharts.log
    

    或者在脚本中指定json文件的路径:

    #!/bin/sh
    for i in /path/to/json/*.json; do
        filename="${i%.*}"
        phantomjs /var/www/highcharts.com/exporting-server/phantomjs/highcharts-convert.js -infile $i -outfile img/$filename.png -scale 2.5 -width 300 -constr Chart -callback /var/www/highcharts.com/exporting-server/phantomjs/callback.js
    done
    

相关问题