HelloWorld
HelloWorld

Reputation: 29

Shell script runs from CLI but not from Crontab

I have a shell script that goes through every JSon file in a directory and uses phantomJS to create a highchart png.

The problem comes when scheduling a cron task to run this script - (Initially I used inotifywait but got the same error).

The shell script looks like this:

#!/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

and the cron task looks like this:

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

In the log file I'm getting the error:

"Unable to open file '*.json'"

The shell script runs fine when run from the command line, but the problem comes when trying to schedule it.

Upvotes: 2

Views: 985

Answers (1)

dogbane
dogbane

Reputation: 274778

Cron runs your commands in your home directory. I'm assuming that the json files are not in your home directory, so your script fails with that error.

Either change your cron job to cd to the directory:

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

Or specify the path to the json files in your script:

#!/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

Upvotes: 3

Related Questions