The Node.js child process module provides the ability to spawn child processes in a similar manner to popen(3).
There are three major way to create child process:
The child_process.exec() method runs a command in a console and buffers the output.
Syntax:
child_process.exec(command[, options], callback)
Parameters:
1) command: It specifies the command to run, with space-separated arguments.
2) options: It may contain one or more of the following options:
callback: The callback function specifies three arguments error, stdout and stderr which is called with the following output when process terminates.
Let's see the simple process example to print architecture, pid, platform and version of the process.
File: child_process_example1.js
const exec = require('child_process').exec; exec('my.bat', (err, stdout, stderr) => { if (err) { console.error(err); return; } console.log(stdout); });
Create a batch file named my.bat having the following code:
File: my.bat
dir mkdir child
Open Node.js command prompt and run the following code:
node child_process_example1.js
It will execute two commands dir and mkdir child. The dir command will display list of current directory and mkdir command will create a new directory. For linux, you can you ls command to display the current directory list.
It will create a new directory also.
Create two js files named support.js and master.js, having the following code:
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
const fs = require('fs'); const child_process = require('child_process'); for(var i=0; i<3; i++) { var workerProcess = child_process.exec('node support.js '+i, function (error, stdout, stderr) { if (error) { console.log(error.stack); console.log('Error code: '+error.code); console.log('Signal received: '+error.signal); } console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); }); workerProcess.on('exit', function (code) { console.log('Child process exited with exit code '+code); }); }
Open Node.js command prompt and run the following code:
node master.js
The child_process.spawn() method launches a new process with a given command. This method returns streams (stdout & stderr) and it is generally used when the process returns large amount of data.
Syntax:
child_process.spawn(command[, args][, options])
Parameters:
1) command: It specifies the command to run.
2) args: It specifies an array List of string arguments.
3) options: It may contain one or more of the following options:
Create two js files named support.js and master.js, having the following code:
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
const fs = require('fs'); const child_process = require('child_process'); for(var i=0; i<3; i++) { var workerProcess = child_process.spawn('node', ['support.js', i]); workerProcess.stdout.on('data', function (data) { console.log('stdout: ' + data); }); workerProcess.stderr.on('data', function (data) { console.log('stderr: ' + data); }); workerProcess.on('close', function (code) { console.log('child process exited with code ' + code); }); }
Open Node.js command prompt and run the following code:
node master.js
The child_process.fork method is a special case of the spawn() to create Node processes. This method returns object with a built-in communication channel in addition to having all the methods in a normal ChildProcess instance.
Syntax:
child_process.fork(modulePath[, args][, options])
Parameters:
1) modulePath: This is a string specifies the module to run in the child.
2) args: It specifies an array List of string arguments.
3) options: It may contain one or more of the following options:
Create two js files named support.js and master.js, having the following code:
File: support.js
const fs = require('fs'); const child_process = require('child_process'); for(var i=0; i<3; i++) { var worker_process = child_process.fork("support.js", [i]); worker_process.on('close', function (code) { console.log('child process exited with code ' + code); }); }
Open Node.js command prompt and run the following code:
node master.js