M Loksly
M Loksly
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.

  1. 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
  2. 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.

  1. You may use a cron command like:
    */1 * * * * cd yourpath; /usr/local/bin/node cronNodeScript.js
  2. 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.

)

    M Loksly

    Written by

    M Loksly

    MongoDB Certified DBA, StrongLoop Certified Node Developer (SCND). Interested in Node.JS, AngularJS, MongoDB, other NoSQL database engines and Cloud Computing