Aug 22, 2017 · 1 min read
Hi Peter,
I have a some comments about your post.
It’s great to introduce node-cron, I’ve used in the past and works right. Your example is what it’s not right.
- NodeJS uses local resolution for require function when file starts by dot. That means that there’s no need to run a nodejs script changing the current working directory (CWD). That means that (cronNodeScript.js#L5)
var shell = require(‘./child_helper’);
won’t fail running it outside with a command like:
/usr/local/bin/node /tmp/nodejs-cron-job-must-know/cronNodeScript.js - I guess by your repository example (scriptParam.js) that what you meant was to access some local files like you do on cronNodeScript.js#L8 and like cronNodeScript.js#L9 . If you run them from wrong CWD then I will fail for sure.
I can give you a two simple solutions to this.
- You may use a cron command like:
*/1 * * * * cd yourpath; /usr/local/bin/node cronNodeScript.js - Change cronNodeScript.js#L8 to something like
‘/usr/local/bin/node ‘ + path.join(path.dirname(__filename), ‘script1.js’). You may also need to require(‘path’) module.
Have a nice day.