2012-02-27 20:04:08 +01:00
|
|
|
# Child Process
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-03 00:14:03 +01:00
|
|
|
Stability: 3 - Stable
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
Node provides a tri-directional `popen(3)` facility through the
|
|
|
|
`child_process` module.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
It is possible to stream data through a child's `stdin`, `stdout`, and
|
2010-10-28 14:18:16 +02:00
|
|
|
`stderr` in a fully non-blocking way.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
To create a child process use `require('child_process').spawn()` or
|
|
|
|
`require('child_process').fork()`. The semantics of each are slightly
|
|
|
|
different, and explained below.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
## Class: ChildProcess
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
`ChildProcess` is an [EventEmitter][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
Child processes always have three streams associated with them. `child.stdin`,
|
|
|
|
`child.stdout`, and `child.stderr`. These may be shared with the stdio
|
|
|
|
streams of the parent process, or they may be separate stream objects
|
|
|
|
which can be piped to and from.
|
|
|
|
|
|
|
|
The ChildProcess class is not intended to be used directly. Use the
|
|
|
|
`spawn()` or `fork()` methods to create a Child Process instance.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### Event: 'exit'
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
* `code` {Number} the exit code, if it exited normally.
|
|
|
|
* `signal` {String} the signal passed to kill the child process, if it
|
|
|
|
was killed by the parent.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
This event is emitted after the child process ends. If the process terminated
|
|
|
|
normally, `code` is the final exit code of the process, otherwise `null`. If
|
|
|
|
the process terminated due to receipt of a signal, `signal` is the string name
|
|
|
|
of the signal, otherwise `null`.
|
|
|
|
|
2012-03-16 01:09:47 +01:00
|
|
|
Note that the child process stdio streams might still be open.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
See `waitpid(2)`.
|
|
|
|
|
2012-03-16 01:09:47 +01:00
|
|
|
### Event: 'close'
|
|
|
|
|
|
|
|
This event is emitted when the stdio streams of a child process have all
|
|
|
|
terminated. This is distinct from 'exit', since multiple processes
|
|
|
|
might share the same stdio streams.
|
|
|
|
|
2012-01-30 16:35:05 +01:00
|
|
|
### Event: 'disconnect'
|
|
|
|
|
|
|
|
This event is emitted after using the `.disconnect()` method in the parent or
|
|
|
|
in the child. After disconnecting it is no longer possible to send messages.
|
|
|
|
An alternative way to check if you can send messages is to see if the
|
|
|
|
`child.connected` property is `true`.
|
|
|
|
|
2012-04-12 09:18:12 +02:00
|
|
|
### Event: 'message'
|
|
|
|
|
|
|
|
* `message` {Object} a parsed JSON object or primitive value
|
2012-04-12 09:23:07 +02:00
|
|
|
* `sendHandle` {Handle object} a Socket or Server object
|
2012-04-12 09:18:12 +02:00
|
|
|
|
|
|
|
Messages send by `.send(message, [sendHandle])` are obtained using the
|
|
|
|
`message` event.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### child.stdin
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
* {Stream object}
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
A `Writable Stream` that represents the child process's `stdin`.
|
|
|
|
Closing this stream via `end()` often causes the child process to terminate.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
If the child stdio streams are shared with the parent, then this will
|
|
|
|
not be set.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### child.stdout
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
* {Stream object}
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
A `Readable Stream` that represents the child process's `stdout`.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
If the child stdio streams are shared with the parent, then this will
|
|
|
|
not be set.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### child.stderr
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
* {Stream object}
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
A `Readable Stream` that represents the child process's `stderr`.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
If the child stdio streams are shared with the parent, then this will
|
|
|
|
not be set.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
### child.pid
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
* {Integer}
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
The PID of the child process.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
var spawn = require('child_process').spawn,
|
|
|
|
grep = spawn('grep', ['ssh']);
|
|
|
|
|
|
|
|
console.log('Spawned child pid: ' + grep.pid);
|
|
|
|
grep.stdin.end();
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
### child.kill([signal])
|
|
|
|
|
|
|
|
* `signal` {String}
|
|
|
|
|
|
|
|
Send a signal to the child process. If no argument is given, the process will
|
|
|
|
be sent `'SIGTERM'`. See `signal(7)` for a list of available signals.
|
|
|
|
|
|
|
|
var spawn = require('child_process').spawn,
|
|
|
|
grep = spawn('grep', ['ssh']);
|
|
|
|
|
|
|
|
grep.on('exit', function (code, signal) {
|
|
|
|
console.log('child process terminated due to receipt of signal '+signal);
|
|
|
|
});
|
|
|
|
|
|
|
|
// send SIGHUP to process
|
|
|
|
grep.kill('SIGHUP');
|
|
|
|
|
|
|
|
Note that while the function is called `kill`, the signal delivered to the child
|
|
|
|
process may not actually kill it. `kill` really just sends a signal to a process.
|
|
|
|
|
|
|
|
See `kill(2)`
|
|
|
|
|
2013-01-18 19:17:26 +01:00
|
|
|
### child.send(message, [sendHandle])
|
2012-02-27 20:04:08 +01:00
|
|
|
|
|
|
|
* `message` {Object}
|
|
|
|
* `sendHandle` {Handle object}
|
|
|
|
|
2012-04-12 09:23:07 +02:00
|
|
|
When using `child_process.fork()` you can write to the child using
|
2012-04-12 09:18:12 +02:00
|
|
|
`child.send(message, [sendHandle])` and messages are received by
|
|
|
|
a `'message'` event on the child.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
var cp = require('child_process');
|
|
|
|
|
|
|
|
var n = cp.fork(__dirname + '/sub.js');
|
|
|
|
|
|
|
|
n.on('message', function(m) {
|
|
|
|
console.log('PARENT got message:', m);
|
|
|
|
});
|
|
|
|
|
|
|
|
n.send({ hello: 'world' });
|
|
|
|
|
|
|
|
And then the child script, `'sub.js'` might look like this:
|
|
|
|
|
|
|
|
process.on('message', function(m) {
|
|
|
|
console.log('CHILD got message:', m);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.send({ foo: 'bar' });
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2012-04-12 09:18:12 +02:00
|
|
|
In the child the `process` object will have a `send()` method, and `process`
|
|
|
|
will emit objects each time it receives a message on its channel.
|
|
|
|
|
|
|
|
There is a special case when sending a `{cmd: 'NODE_foo'}` message. All messages
|
|
|
|
containing a `NODE_` prefix in its `cmd` property will not be emitted in
|
|
|
|
the `message` event, since they are internal messages used by node core.
|
|
|
|
Messages containing the prefix are emitted in the `internalMessage` event, you
|
|
|
|
should by all means avoid using this feature, it is subject to change without notice.
|
|
|
|
|
2012-04-12 09:23:07 +02:00
|
|
|
The `sendHandle` option to `child.send()` is for sending a TCP server or
|
|
|
|
socket object to another process. The child will receive the object as its
|
|
|
|
second argument to the `message` event.
|
|
|
|
|
2013-01-18 19:17:26 +01:00
|
|
|
#### Example: sending server object
|
2012-04-12 09:23:07 +02:00
|
|
|
|
|
|
|
Here is an example of sending a server:
|
|
|
|
|
|
|
|
var child = require('child_process').fork('child.js');
|
|
|
|
|
|
|
|
// Open up the server object and send the handle.
|
|
|
|
var server = require('net').createServer();
|
|
|
|
server.on('connection', function (socket) {
|
|
|
|
socket.end('handled by parent');
|
|
|
|
});
|
|
|
|
server.listen(1337, function() {
|
|
|
|
child.send('server', server);
|
|
|
|
});
|
|
|
|
|
2012-08-04 21:45:50 +02:00
|
|
|
And the child would the receive the server object as:
|
2012-04-12 09:23:07 +02:00
|
|
|
|
|
|
|
process.on('message', function(m, server) {
|
|
|
|
if (m === 'server') {
|
|
|
|
server.on('connection', function (socket) {
|
|
|
|
socket.end('handled by child');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Note that the server is now shared between the parent and child, this means
|
|
|
|
that some connections will be handled by the parent and some by the child.
|
|
|
|
|
2013-01-18 19:17:26 +01:00
|
|
|
#### Example: sending socket object
|
2012-04-12 09:23:07 +02:00
|
|
|
|
2012-08-04 21:45:50 +02:00
|
|
|
Here is an example of sending a socket. It will spawn two children and handle
|
2012-04-12 09:23:07 +02:00
|
|
|
connections with the remote address `74.125.127.100` as VIP by sending the
|
|
|
|
socket to a "special" child process. Other sockets will go to a "normal" process.
|
|
|
|
|
|
|
|
var normal = require('child_process').fork('child.js', ['normal']);
|
|
|
|
var special = require('child_process').fork('child.js', ['special']);
|
|
|
|
|
|
|
|
// Open up the server and send sockets to child
|
|
|
|
var server = require('net').createServer();
|
|
|
|
server.on('connection', function (socket) {
|
|
|
|
|
|
|
|
// if this is a VIP
|
|
|
|
if (socket.remoteAddress === '74.125.127.100') {
|
|
|
|
special.send('socket', socket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// just the usual dudes
|
|
|
|
normal.send('socket', socket);
|
|
|
|
});
|
|
|
|
server.listen(1337);
|
|
|
|
|
|
|
|
The `child.js` could look like this:
|
|
|
|
|
|
|
|
process.on('message', function(m, socket) {
|
|
|
|
if (m === 'socket') {
|
2012-09-02 09:36:21 +02:00
|
|
|
socket.end('You were handled as a ' + process.argv[2] + ' person');
|
2012-04-12 09:23:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Note that once a single socket has been sent to a child the parent can no
|
|
|
|
longer keep track of when the socket is destroyed. To indicate this condition
|
|
|
|
the `.connections` property becomes `null`.
|
2012-08-04 21:45:50 +02:00
|
|
|
It is also recommended not to use `.maxConnections` in this condition.
|
2012-04-12 09:18:12 +02:00
|
|
|
|
|
|
|
### child.disconnect()
|
|
|
|
|
|
|
|
To close the IPC connection between parent and child use the
|
|
|
|
`child.disconnect()` method. This allows the child to exit gracefully since
|
|
|
|
there is no IPC channel keeping it alive. When calling this method the
|
|
|
|
`disconnect` event will be emitted in both parent and child, and the
|
|
|
|
`connected` flag will be set to `false`. Please note that you can also call
|
|
|
|
`process.disconnect()` in the child process.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
## child_process.spawn(command, [args], [options])
|
|
|
|
|
|
|
|
* `command` {String} The command to run
|
|
|
|
* `args` {Array} List of string arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
2012-05-16 18:04:24 +02:00
|
|
|
* `stdio` {Array|String} Child's stdio configuration. (See below)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `customFds` {Array} **Deprecated** File descriptors for the child to use
|
|
|
|
for stdio. (See below)
|
|
|
|
* `env` {Object} Environment key-value pairs
|
2012-06-01 06:23:05 +02:00
|
|
|
* `detached` {Boolean} The child will be a process group leader. (See below)
|
2012-10-23 18:45:10 +02:00
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
2012-02-27 20:04:08 +01:00
|
|
|
* return: {ChildProcess object}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Launches a new process with the given `command`, with command line arguments in `args`.
|
|
|
|
If omitted, `args` defaults to an empty Array.
|
|
|
|
|
|
|
|
The third argument is used to specify additional options, which defaults to:
|
|
|
|
|
2010-11-18 03:25:14 +01:00
|
|
|
{ cwd: undefined,
|
2012-02-26 20:38:36 +01:00
|
|
|
env: process.env
|
2011-01-11 01:22:58 +01:00
|
|
|
}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
`cwd` allows you to specify the working directory from which the process is spawned.
|
|
|
|
Use `env` to specify environment variables that will be visible to the new process.
|
2011-10-11 20:27:58 +02:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the exit code:
|
|
|
|
|
2012-08-11 19:56:04 +02:00
|
|
|
var spawn = require('child_process').spawn,
|
2010-10-28 14:18:16 +02:00
|
|
|
ls = spawn('ls', ['-lh', '/usr']);
|
|
|
|
|
|
|
|
ls.stdout.on('data', function (data) {
|
2010-11-08 02:22:36 +01:00
|
|
|
console.log('stdout: ' + data);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ls.stderr.on('data', function (data) {
|
2010-11-08 02:22:36 +01:00
|
|
|
console.log('stderr: ' + data);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ls.on('exit', function (code) {
|
|
|
|
console.log('child process exited with code ' + code);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Example: A very elaborate way to run 'ps ax | grep ssh'
|
|
|
|
|
2012-08-11 19:56:04 +02:00
|
|
|
var spawn = require('child_process').spawn,
|
2010-10-28 14:18:16 +02:00
|
|
|
ps = spawn('ps', ['ax']),
|
|
|
|
grep = spawn('grep', ['ssh']);
|
|
|
|
|
|
|
|
ps.stdout.on('data', function (data) {
|
|
|
|
grep.stdin.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.stderr.on('data', function (data) {
|
2010-11-08 02:22:36 +01:00
|
|
|
console.log('ps stderr: ' + data);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2012-07-13 23:18:52 +02:00
|
|
|
console.log('' + data);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
grep.stderr.on('data', function (data) {
|
2010-11-08 02:22:36 +01:00
|
|
|
console.log('grep stderr: ' + data);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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('child_process').spawn,
|
|
|
|
child = spawn('bad_command');
|
|
|
|
|
2011-03-23 23:01:49 +01:00
|
|
|
child.stderr.setEncoding('utf8');
|
2010-10-28 14:18:16 +02:00
|
|
|
child.stderr.on('data', function (data) {
|
2011-03-23 23:01:49 +01:00
|
|
|
if (/^execvp\(\)/.test(data)) {
|
2010-10-28 14:18:16 +02:00
|
|
|
console.log('Failed to start child process.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-09-29 09:53:36 +02:00
|
|
|
Note that if spawn receives an empty options object, it will result in
|
|
|
|
spawning the process with an empty environment rather than using
|
|
|
|
`process.env`. This due to backwards compatibility issues with a deprecated
|
|
|
|
API.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-05-16 18:04:24 +02:00
|
|
|
The 'stdio' option to `child_process.spawn()` is an array where each
|
|
|
|
index corresponds to a fd in the child. The value is one of the following:
|
|
|
|
|
2012-06-04 14:04:15 +02:00
|
|
|
1. `'pipe'` - Create a pipe between the child process and the parent process.
|
|
|
|
The parent end of the pipe is exposed to the parent as a property on the
|
2012-06-01 06:23:05 +02:00
|
|
|
`child_process` object as `ChildProcess.stdio[fd]`. Pipes created for
|
2012-06-04 14:04:15 +02:00
|
|
|
fds 0 - 2 are also available as ChildProcess.stdin, ChildProcess.stdout
|
|
|
|
and ChildProcess.stderr, respectively.
|
|
|
|
2. `'ipc'` - Create an IPC channel for passing messages/file descriptors
|
|
|
|
between parent and child. A ChildProcess may have at most *one* IPC stdio
|
|
|
|
file descriptor. Setting this option enables the ChildProcess.send() method.
|
|
|
|
If the child writes JSON messages to this file descriptor, then this will
|
|
|
|
trigger ChildProcess.on('message'). If the child is a Node.js program, then
|
|
|
|
the presence of an IPC channel will enable process.send() and
|
|
|
|
process.on('message').
|
|
|
|
3. `'ignore'` - Do not set this file descriptor in the child. Note that Node
|
|
|
|
will always open fd 0 - 2 for the processes it spawns. When any of these is
|
|
|
|
ignored node will open `/dev/null` and attach it to the child's fd.
|
|
|
|
4. `Stream` object - Share a readable or writable stream that refers to a tty,
|
|
|
|
file, socket, or a pipe with the child process. The stream's underlying
|
|
|
|
file descriptor is duplicated in the child process to the fd that
|
|
|
|
corresponds to the index in the `stdio` array.
|
|
|
|
5. Positive integer - The integer value is interpreted as a file descriptor
|
|
|
|
that is is currently open in the parent process. It is shared with the child
|
|
|
|
process, similar to how `Stream` objects can be shared.
|
|
|
|
6. `null`, `undefined` - Use default value. For stdio fds 0, 1 and 2 (in other
|
|
|
|
words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the
|
|
|
|
default is `'ignore'`.
|
2012-05-16 18:04:24 +02:00
|
|
|
|
|
|
|
As a shorthand, the `stdio` argument may also be one of the following
|
|
|
|
strings, rather than an array:
|
|
|
|
|
|
|
|
* `ignore` - `['ignore', 'ignore', 'ignore']`
|
|
|
|
* `pipe` - `['pipe', 'pipe', 'pipe']`
|
|
|
|
* `inherit` - `[process.stdin, process.stdout, process.stderr]` or `[0,1,2]`
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
// Child will use parent's stdios
|
|
|
|
spawn('prg', [], { stdio: 'inherit' });
|
|
|
|
|
|
|
|
// Spawn child sharing only stderr
|
|
|
|
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
|
|
|
|
|
|
|
|
// Open an extra fd=4, to interact with programs present a
|
|
|
|
// startd-style interface.
|
|
|
|
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
|
|
|
|
|
2012-06-01 06:23:05 +02:00
|
|
|
If the `detached` option is set, the child process will be made the leader of a
|
|
|
|
new process group. This makes it possible for the child to continue running
|
|
|
|
after the parent exits.
|
|
|
|
|
|
|
|
By default, the parent will wait for the detached child to exit. To prevent
|
|
|
|
the parent from waiting for a given `child`, use the `child.unref()` method,
|
|
|
|
and the parent's event loop will not include the child in its reference count.
|
|
|
|
|
|
|
|
Example of detaching a long-running process and redirecting its output to a
|
|
|
|
file:
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
spawn = require('child_process').spawn,
|
|
|
|
out = fs.openSync('./out.log', 'a'),
|
|
|
|
err = fs.openSync('./out.log', 'a');
|
|
|
|
|
|
|
|
var child = spawn('prg', [], {
|
2012-06-25 18:53:35 +02:00
|
|
|
detached: true,
|
2012-06-01 06:23:05 +02:00
|
|
|
stdio: [ 'ignore', out, err ]
|
|
|
|
});
|
|
|
|
|
|
|
|
child.unref();
|
|
|
|
|
|
|
|
When using the `detached` option to start a long-running process, the process
|
|
|
|
will not stay running in the background unless it is provided with a `stdio`
|
|
|
|
configuration that is not connected to the parent. If the parent's `stdio` is
|
|
|
|
inherited, the child will remain attached to the controlling terminal.
|
|
|
|
|
2011-11-04 23:55:06 +01:00
|
|
|
There is a deprecated option called `customFds` which allows one to specify
|
2011-11-05 04:35:38 +01:00
|
|
|
specific file descriptors for the stdio of the child process. This API was
|
|
|
|
not portable to all platforms and therefore removed.
|
2012-02-27 20:04:08 +01:00
|
|
|
With `customFds` it was possible to hook up the new process' `[stdin, stdout,
|
|
|
|
stderr]` to existing streams; `-1` meant that a new stream should be created.
|
2011-11-04 23:55:06 +01:00
|
|
|
Use at your own risk.
|
|
|
|
|
|
|
|
There are several internal options. In particular `stdinStream`,
|
|
|
|
`stdoutStream`, `stderrStream`. They are for INTERNAL USE ONLY. As with all
|
|
|
|
undocumented APIs in Node, they should not be used.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
See also: `child_process.exec()` and `child_process.fork()`
|
|
|
|
|
|
|
|
## child_process.exec(command, [options], callback)
|
|
|
|
|
|
|
|
* `command` {String} The command to run, with space-separated arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
2012-05-16 18:04:24 +02:00
|
|
|
* `stdio` {Array|String} Child's stdio configuration. (See above)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `customFds` {Array} **Deprecated** File descriptors for the child to use
|
2012-05-16 18:04:24 +02:00
|
|
|
for stdio. (See above)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `encoding` {String} (Default: 'utf8')
|
|
|
|
* `timeout` {Number} (Default: 0)
|
|
|
|
* `maxBuffer` {Number} (Default: 200*1024)
|
|
|
|
* `killSignal` {String} (Default: 'SIGTERM')
|
|
|
|
* `callback` {Function} called with the output when process terminates
|
2012-05-01 04:41:29 +02:00
|
|
|
* `error` {Error}
|
2012-02-27 20:04:08 +01:00
|
|
|
* `stdout` {Buffer}
|
|
|
|
* `stderr` {Buffer}
|
|
|
|
* Return: ChildProcess object
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-09-14 05:50:47 +02:00
|
|
|
Runs a command in a shell and buffers the output.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-07-31 21:38:58 +02:00
|
|
|
var exec = require('child_process').exec,
|
2010-10-28 14:18:16 +02:00
|
|
|
child;
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
child = exec('cat *.js bad_file | wc -l',
|
2010-10-28 14:18:16 +02:00
|
|
|
function (error, stdout, stderr) {
|
2010-11-08 02:22:36 +01:00
|
|
|
console.log('stdout: ' + stdout);
|
|
|
|
console.log('stderr: ' + stderr);
|
2010-10-28 14:18:16 +02:00
|
|
|
if (error !== null) {
|
|
|
|
console.log('exec error: ' + error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
The callback gets the arguments `(error, stdout, stderr)`. On success, `error`
|
|
|
|
will be `null`. On error, `error` will be an instance of `Error` and `err.code`
|
|
|
|
will be the exit code of the child process, and `err.signal` will be set to the
|
|
|
|
signal that terminated the process.
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
There is a second optional argument to specify several options. The
|
|
|
|
default options are
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-18 03:25:14 +01:00
|
|
|
{ encoding: 'utf8',
|
|
|
|
timeout: 0,
|
|
|
|
maxBuffer: 200*1024,
|
|
|
|
killSignal: 'SIGTERM',
|
|
|
|
cwd: null,
|
|
|
|
env: null }
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
If `timeout` is greater than 0, then it will kill the child process
|
|
|
|
if it runs longer than `timeout` milliseconds. The child process is killed with
|
|
|
|
`killSignal` (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.
|
|
|
|
|
|
|
|
|
2012-02-27 20:04:08 +01:00
|
|
|
## child_process.execFile(file, args, options, callback)
|
|
|
|
|
|
|
|
* `file` {String} The filename of the program to run
|
|
|
|
* `args` {Array} List of string arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
2012-05-16 18:04:24 +02:00
|
|
|
* `stdio` {Array|String} Child's stdio configuration. (See above)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `customFds` {Array} **Deprecated** File descriptors for the child to use
|
2012-05-16 18:04:24 +02:00
|
|
|
for stdio. (See above)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `encoding` {String} (Default: 'utf8')
|
|
|
|
* `timeout` {Number} (Default: 0)
|
2012-06-06 21:05:18 +02:00
|
|
|
* `maxBuffer` {Number} (Default: 200\*1024)
|
2012-02-27 20:04:08 +01:00
|
|
|
* `killSignal` {String} (Default: 'SIGTERM')
|
|
|
|
* `callback` {Function} called with the output when process terminates
|
2012-05-01 04:41:29 +02:00
|
|
|
* `error` {Error}
|
2012-02-27 20:04:08 +01:00
|
|
|
* `stdout` {Buffer}
|
|
|
|
* `stderr` {Buffer}
|
|
|
|
* Return: ChildProcess object
|
2011-09-14 05:50:47 +02:00
|
|
|
|
|
|
|
This is similar to `child_process.exec()` except it does not execute a
|
|
|
|
subshell but rather the specified file directly. This makes it slightly
|
|
|
|
leaner than `child_process.exec`. It has the same options.
|
|
|
|
|
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
## child\_process.fork(modulePath, [args], [options])
|
2012-02-27 20:04:08 +01:00
|
|
|
|
|
|
|
* `modulePath` {String} The module to run in the child
|
|
|
|
* `args` {Array} List of string arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `encoding` {String} (Default: 'utf8')
|
2013-01-04 20:58:35 +01:00
|
|
|
* `execPath` {String} Executable used to create the child process
|
2012-02-27 20:04:08 +01:00
|
|
|
* Return: ChildProcess object
|
2011-05-11 09:41:16 +02:00
|
|
|
|
|
|
|
This is a special case of the `spawn()` functionality for spawning Node
|
|
|
|
processes. In addition to having all the methods in a normal ChildProcess
|
2012-04-12 09:23:07 +02:00
|
|
|
instance, the returned object has a communication channel built-in. See
|
2012-04-12 09:18:12 +02:00
|
|
|
`child.send(message, [sendHandle])` for details.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
2011-12-17 11:52:40 +01:00
|
|
|
By default the spawned Node process will have the stdout, stderr associated
|
|
|
|
with the parent's. To change this behavior set the `silent` property in the
|
|
|
|
`options` object to `true`.
|
2011-05-11 21:31:35 +02:00
|
|
|
|
2012-07-31 03:09:04 +02:00
|
|
|
The child process does not automatically exit once it's done, you need to call
|
|
|
|
`process.exit()` explicitly. This limitation may be lifted in the future.
|
|
|
|
|
2011-05-11 21:31:35 +02:00
|
|
|
These child Nodes are still whole new instances of V8. Assume at least 30ms
|
|
|
|
startup and 10mb memory for each new Node. That is, you cannot create many
|
|
|
|
thousands of them.
|
2012-06-06 21:05:18 +02:00
|
|
|
|
2013-01-04 20:58:35 +01:00
|
|
|
The `execPath` property in the `options` object allows for a process to be
|
|
|
|
created for the child rather than the current `node` executable. This should be
|
|
|
|
done with care and by default will talk over the fd represented an
|
|
|
|
environmental variable `NODE_CHANNEL_FD` on the child process. The input and
|
|
|
|
output on this fd is expected to be line delimited JSON objects.
|
|
|
|
|
2012-06-06 21:05:18 +02:00
|
|
|
[EventEmitter]: events.html#events_class_events_eventemitter
|