home PYTHONJAVA
 

Node.js Child Process


A provides of ordinarily utilized Node.js process capacities are given below.

FunctionDescription
cwd()returns way of current working directory
hrtime()returns the present high-goals constant in a [seconds, nanoseconds] array
memoryUsage()returns an article having data of memory usage.
process.kill(pid[, signal])is used to execute the given pid.
uptime()returns the Node.js procedure uptime in seconds.

Node.js Process Functions Example

Let's see the procedure example to print current working registry and uptime of the process.

File: process_example3.js

  1. console.log('Current directory: ${process.cwd()}');  
  2. console.log('Uptime: ${process.uptime()}');  

Open Node.js direction brief and run the accompanying code:

The Node.js tyke process module gives the capacity to produce youngster forms likewise to popen(3).

There are three noteworthy approach to make tyke process:

  • child_process.exec() method: This technique runs an order in a support and cradles the yield.
  • child_process.spawn() method: This method dispatches another procedure with a given direction.
  • child_process.fork() method: This method is a unique instance of generate() technique to make kid forms.

Node.js child_process.exec() method

The child_process.exec() method runs an order in a comfort and supports the yield.

Syntax:

  1. child_process.exec(command[, options], callback)  

Parameters:

1) order: It indicates the direction to run, with space-isolated arguments.

2) alternatives: It might contain at least one of the accompanying options:

  • cwd: It determines the present working catalog of the tyke process.
  • env: It determines condition key-esteem pairs.
  • encoding: String (Default: 'utf8')
  • shell: It determines string Shell to execute the direction with (Default: '/receptacle/sh' on UNIX, 'cmd.exe' on Windows, The shell ought to comprehend the - c switch on UNIX or/s/c on Windows. On Windows, order line parsing ought to be perfect with cmd.exe.)
  • timeout: Number (Default: 0)
  • maxBuffer: Number (Default: 200*1024)
  • killSignal: String (Default: 'SIGTERM')
  • uid Number: Sets the client character of the process.
  • gid Number: Sets the gathering character of the process.

callback: The callback work indicates three contentions blunder, stdout and stderr which is called with the accompanying yield when process terminates.

Node.js child_process.exec() model 1

Let's see the basic procedure example to print engineering, pid, stage and version of the process.

File: child_process_example1.js

  1. const exec = require('child_process').exec;  
  2. exec('my.bat', (err, stdout, stderr) => {  
  3.   if (err) {  
  4.     console.error(err);  
  5.     return;  
  6.   }  
  7.   console.log(stdout);  
  8. });  

Create a clump record named my.bat having the accompanying code:

File: my.bat

  1. dir  
  2. mkdir child  

Open Node.js direction brief and run the accompanying code:

  1. node child_process_example1.js  

It will execute two directions dir and mkdir child. The dir order will show rundown of current catalog and mkdir order will make another index. For linux, you can you ls order to show the present index list.

Node.js child_process.exec() example 2

Create two js files named support.js and master.js, having the following code:

File: support.js

  1. console.log("Child Process " + process.argv[2] + " executed." );  

File: master.js

  1. const fs = require('fs');  
  2. const child_process = require('child_process');  
  3. for(var i=0; i<3; i++) {  
  4.    var workerProcess = child_process.exec('node support.js '+i,  
  5.       function (error, stdout, stderr) {  
  6.          if (error) {  
  7.             console.log(error.stack);  
  8.             console.log('Error code: '+error.code);  
  9.             console.log('Signal received: '+error.signal);  
  10.          }  
  11.          console.log('stdout: ' + stdout);  
  12.          console.log('stderr: ' + stderr);  
  13.       });  
  14.       workerProcess.on('exit', function (code) {  
  15.       console.log('Child process exited with exit code '+code);  
  16.    });  
  17. }  

Open Node.js command prompt and run the following code:

  1. node master.js  
Node.js child process exec() example 1

Node.js child_process.spawn() method

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:

  1. 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:

  • cwd: It specifies the current working directory of the child process.
  • env: It specifies environment key-value pairs.
  • stdio: Array|String Child's stdio configuration
  • customFds: Array Deprecated File descriptors for the child to use for stdio
  • detached Boolean : The child will be a process group leader
  • uid Number: Sets the user identity of the process.
  • gid Number: Sets the group identity of the process.





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.