user16891328 user16891328 - 3 months ago 9
Node.js Question

Run node script from within a node script

I would like to use node-schedule to run a certain node script at a certain time. Can I do something like this in node.js?

var schedule = require('node-schedule');

//https://www.npmjs.com/package/node-schedule
var j = schedule.scheduleJob('00 00 22 * * *', function () {
console.log('Running XX node.js script ...');

NodeShell.run(__dirname + '\\XX.js', function (err) {
if (err) throw err;
console.log('finished');
});
});


Not sure if something like
NodeShell
exists.

Answer

You have several options, all of which are listed in the docs for child_process. Briefly:

  • child_process.exec spawns a shell
  • child_process.fork forks a node process
  • child_process.spawn spawns a process
Comments