2013-03-19 151 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