Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
 


  

 
Child Process EXEC
Question Posted on 30 Mar 2024

Home >> Tutorial >> Node.js Tutorial >> Child Process EXEC


Child Process EXEC

Node.js child_process.exec() method


This method will runs a command in console and buffers the output. And we have tried provide some example to cover this point.

Syntax:-


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


Parameters:-

(1)command:-First parameter in above method is command. And this specifies the command to run, with space-separated arguments.

(2)options:-When we say about the options this may contain one or more of the below options:-
  • cwd:-Here we will specifies the current working directory of the child process.

  • env:-Here we will specifies the environment key-vakue pairs.

  • encoding:-Here we will specifies encoding algo like we mention String (Default: 'utf8')

  • shell:-It specifies string Shell to execute the command with (Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows, The shell should understand the -c switch on UNIX or /s /c on Windows. On Windows, command line parsing should be compatible with cmd.exe.)

  • timeout:-Here we will specifies the Number (Default: 0)

  • maxBuffer:-Here we will specifies the Number (Default: 200*1024)

  • killSignal:-Here we will specifies the String (Default: 'SIGTERM')

  • uid Number:-Here we will sets the user identity of the process.

  • gid Number:-Here we will sets the group identity of the process.


(3)callback:-Here on callabck parameter,this callabck function specifies three argument error, stdout and stderr which is called with the following output when process terminates.


Node.js child_process.exec() example 1


Now jump to first example of child_process. Here we have take the simple process example to print architecture, pid, platform and version of the process.
File: child_processexample1.js


Child Process
Now we will create a batch file named my.bat having the following code:

File: my.bat

dir
mkdir child


Now we will open the Node.js command prompt and run the below code:-

node child_processexample1.js

Once we execute the js file there are two commands executed that are dir and mkdir child. Here dir command will display the list of the current directory and nkdir command will create a new directory. And when we say about the linux you need to run the ls command to display the current directory list.

Below is calling and execution of js file in node.js.


two files exec
And above command will create a new folder.


create folder

Node.js child_process.exec() example 2


Now here in 2nd example we will create two js file and first file is support.js and second file in master.js. With below code:-

File: support.js

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

File: master.js


Node.js master file
Now open Node.js command and then on command prompt run the below command:-

node master.js


MasterFile Run





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |