Created
October 30, 2016 22:37
-
-
Save mnyamor/f97832380de9f93e7d091095e75877c3 to your computer and use it in GitHub Desktop.
Creating Child Process With Exec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- Node.js comes with a Child Process module which allows you to execute external processes in your environment. | |
- In other words, your Node.js app can run and communicate with other applications on the computer that it is hosting | |
- Two main functions used to create Child Processes: spawn and execute | |
- | |
*/ | |
//The Child Process module contains the execute function | |
var exec = require("child_process").exec; | |
//exec('open http'://www.google.com'); | |
/* | |
Every time we've been executing any of these processes any data | |
that gets returned by the process would be returned to the second | |
argument in the execute function, a call back function. | |
So in this call back function after we execute ls and we receive information | |
back from ls it will be injected into this function via argument. | |
So first, if there are any errors when executing a directory listing | |
we pass those as the first argument. | |
The next argument is the stdout, or standard output, | |
that we've received from executing the ls | |
*/ | |
//exec('ls', function(err, stdout) { if (err) { throw err; } console.log("ls finished"); console.log(stdout);}); | |
exec("git version", function(err, stdout) { | |
if (err) { | |
throw err; | |
} | |
console.log("Git Version Executed"); | |
console.log(stdout); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment