Запуск дочернего процесса из Node.JS

Дока по node.js на русском: https://js-node.ru. Раздел по дочерним процессам: https://js-node.ru/site/article?id=13
Документ на сайте webformyself: https://webformyself.com/node-js-masshtabirovanie-prilozheniya-s-pomoshhyu-child_process/


var childProcess = require('child_process');

function runScript(scriptPath, callback) {

// keep track of whether callback has been invoked to prevent multiple invocations
var invoked = false;

var process = childProcess.fork(scriptPath);

// listen for errors as they may prevent the exit event from firing
process.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});

// execute the callback once the process has finished running
process.on('exit', function (code) {
if (invoked) return;
invoked = true;
var err = code === 0 ? null : new Error('exit code ' + code);
callback(err);
});

}

// Now we can run a script and invoke a callback when complete, e.g.
runScript('./some-script.js', function (err) {
if (err) throw err;
console.log('finished running some-script.js');
});

Комментарии

Популярные сообщения из этого блога

Настройка IIS для работы с NodeJS

Kerberos и IIS OOS авторизация

Отладка Angular в VS Code