I'm running into an error when attempting to run a cron job writing a JSON file.
Here's the code within my node file:
var request = require('request');
var fs = require('fs');
var url = "file url";
request.get(url, function(err, response, data) {
if (err || response.statusCode != 200) {
console.log('Error: ' + err);
return;
}
var json = JSON.parse(data);
var processed_json = JSON.stringify(transformJSON(json));
fs.writeFile(__dirname + "/my_data.json", processed_json,
function(err) {
if (err) {
console.log(err);
}
});
});
The error that the cron job gives is:
{ [Error: EACCES, open '.../my_data.json']
errno: 3,
code: 'EACCES',
path: '.../my_data.json' }
The line within my crontab is:
* * * * * node web_scripts/converter.js
Any help would be greatly appreciated!
I tested this on my Mac (Mavericks) and it works for me. The crontab runs and I get my_data.json in the folder where the script lives.
Most likely, the cron job doesn't have permission to write to web_scripts
which is where the script lives. You can usually check /var/log/mail/$YOURUSER to see any cron results.
Here is how I typically set up the cron jobs:
touch ~/.crontab
vi ~/.crontab # add your cronjob
crontab ~/.crontab
Now you can tail the logs and make sure it runs, then check the mail for result. Again, this sounds like a permissions issue. You can also try writing temporarily to path everyone has full perms too, like /tmp
, instead of resolving the path with __dirname
.