Namespace node.child_process
Node provides a tri-directional popen(3)
facility through the ChildProcess
class.
It is possible to stream data through the child's stdin
, stdout
, and
stderr
in a fully non-blocking way.
To create a child process use require('child_process').spawn()
.
Child processes always have three streams associated with them. child.stdin
,
child.stdout
, and child.stderr
.
ChildProcess
is an EventEmitter
.
- Defined in: node.child_process.js
Method Summary
Method Attributes | Method Name and Description |
---|---|
static |
node.child_process.exec(command, options, callback)
High-level way to execute a command as a child process, buffer the
output, and return it all in a callback.
|
static |
node.child_process.execFile(file, options, callback)
|
static |
node.child_process.spawn(path, args, options, customFds)
Launches a new process with the given
command , with command line arguments in args . |
Method Detail
-
static node.child_process.exec(command, options, callback)High-level way to execute a command as a child process, buffer the output, and return it all in a callback.
var util = require('util'), exec = require('childprocess').exec, child; child = exec('cat *.js badfile | wc -l', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } });
The callback gets the arguments(error, stdout, stderr)
. On success,error
will benull
. On error,error
will be an instance ofError
anderr.code
will be the exit code of the child process, anderr.signal
will be set to the signal that terminated the process. There is a second optional argument to specify several options. The default options are{ encoding: 'utf8', timeout: 0, maxBuffer: 200*1024, killSignal: 'SIGTERM', cwd: null, env: null }
Iftimeout
is greater than 0, then it will kill the child process if it runs longer thantimeout
milliseconds. The child process is killed withkillSignal
(default:'SIGTERM'
).maxBuffer
specifies the largest amount of data allowed on stdout or stderr - if this value is exceeded then the child process is killed.- Parameters:
- {string} command
- {Object} options
- {function(Error?|...[*]):undefined=} callback
-
static node.child_process.execFile(file, options, callback)
- Parameters:
- {string} file
- {Object} options
- {function(Error?|...[*]):undefined=} callback
-
static node.child_process.spawn(path, args, options, customFds)Launches a new process with the given
command
, with command line arguments inargs
. If omitted,args
defaults to an empty Array. The third argument is used to specify additional options, which defaults to:{ cwd: undefined, env: process.env, customFds: [-1, -1, -1], setsid: false }
cwd
allows you to specify the working directory from which the process is spawned. Useenv
to specify environment variables that will be visible to the new process. WithcustomFds
it is possible to hook up the new process' [stdin, stout, stderr] to existing streams;-1
means that a new stream should be created.setsid
, if set true, will cause the subprocess to be run in a new session. Example of runningls -lh /usr
, capturingstdout
,stderr
, and the exit code:var util = require('util'), spawn = require('childprocess').spawn, ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', function (data) { console.log('stdout: ' + data); }); ls.stderr.on('data', function (data) { console.log('stderr: ' + data); }); ls.on('exit', function (code) { console.log('child process exited with code ' + code); });
Example: A very elaborate way to run 'ps ax | grep ssh'var util = require('util'), spawn = require('childprocess').spawn, ps = spawn('ps', ['ax']), grep = spawn('grep', ['ssh']); ps.stdout.on('data', function (data) { grep.stdin.write(data); }); ps.stderr.on('data', function (data) { console.log('ps stderr: ' + data); }); ps.on('exit', function (code) { if (code !== 0) { console.log('ps process exited with code ' + code); } grep.stdin.end(); }); grep.stdout.on('data', function (data) { console.log(data); }); grep.stderr.on('data', function (data) { console.log('grep stderr: ' + data); }); grep.on('exit', function (code) { if (code !== 0) { console.log('grep process exited with code ' + code); } });
Example of checking for failed exec:var spawn = require('childprocess').spawn, child = spawn('badcommand'); child.stderr.on('data', function (data) { if (/^execvp\(\)/.test(data.asciiSlice(0,data.length))) { console.log('Failed to start child process.'); } });
See also:child_process.exec()
- Parameters:
- {string} path
- {Array.<*>} args
- {Object} options
- {string} customFds