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. Other alternatives would be welcomed.
You have several options, all of which are listed in the docs for child_process. Briefly:
child_process.exec spawns a shellchild_process.fork forks a node processchild_process.spawn spawns a processFor your case, to run a node script you could use something like this:
var childProcess = require("child_process");
var path = require("path");
var cp = childProcess.fork(path.join(__dirname, "xx.js"));
cp.on("exit", function (code, signal) {
console.log("Exited", {code: code, signal: signal});
});
cp.on("error", console.error.bind(console));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With