2013-03-19 152 views
2

我有一个shell脚本,它通过目录中的每个JSon文件并使用phantomJS创建一个高图表。Shell脚本从CLI运行,但不是从Crontab运行

调度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 

和定期任务看起来是这样的:

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

在我得到的错误日志文件:

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

从命令行运行shell脚本时,shell脚本运行良好,b当试图安排它时,问题就来了。

+0

确保JSON文件的读/写能力由用户在哪个cron运行 – Anubhab 2013-03-19 09:33:43

回答

3

Cron在您的主目录中运行您的命令。我假设json文件不在您的主目录中,因此您的脚本会因此错误而失败。

无论你的cron作业更改到该目录:

* * * * * 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 
+0

我可以发誓我试过这个哈哈..感谢您的快速帮助:) – HelloWorld 2013-03-19 10:51:13