我想使用节点计划在特定时间运行特定节点脚本。我可以在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');
});
});
不知道是否NodeShell
存在类似的东西。其他替代方案将受到欢迎。
您有几个选项,所有选项都在的文档child_process
中列出。简要地:
child_process.exec
产生贝壳child_process.fork
派生一个节点进程child_process.spawn
产生一个过程对于您的情况,要运行节点脚本,可以使用如下所示的内容:
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));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句