2012-02-27 20:04:08 +01:00
|
|
|
# Child Process
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Node.js provides a tri-directional `popen(3)` facility through the
|
2012-02-27 20:04:08 +01:00
|
|
|
`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
|
2013-02-22 01:19:39 +01:00
|
|
|
`stderr` in a fully non-blocking way. (Note that some programs use
|
2015-08-13 18:14:34 +02:00
|
|
|
line-buffered I/O internally. That doesn't affect Node.js but it means
|
2014-07-08 01:38:05 +02:00
|
|
|
data you send to the child process may not be immediately consumed.)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-08 04:31:38 +01:00
|
|
|
To create a child process, use `require('child_process').spawn()` or
|
2012-02-27 20:04:08 +01:00
|
|
|
`require('child_process').fork()`. The semantics of each are slightly
|
2015-12-08 04:31:38 +01:00
|
|
|
different as explained [below][].
|
2014-07-08 01:38:05 +02:00
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
For scripting purposes you may find the [synchronous counterparts][] more
|
2014-07-08 01:38:05 +02:00
|
|
|
convenient.
|
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
|
|
|
|
2015-11-28 00:30:32 +01: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.
|
|
|
|
|
2015-12-03 23:07:01 +01:00
|
|
|
The `ChildProcess` class is not intended to be used directly. Use the
|
|
|
|
[`spawn()`][], [`exec()`][], [`execFile()`][], or [`fork()`][] methods to create
|
|
|
|
an instance of `ChildProcess`.
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### Event: 'close'
|
|
|
|
|
|
|
|
* `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.
|
|
|
|
|
|
|
|
This event is emitted when the stdio streams of a child process have all
|
2015-11-28 00:30:32 +01:00
|
|
|
terminated. This is distinct from `'exit'`, since multiple processes
|
2015-11-04 16:50:18 +01:00
|
|
|
might share the same stdio streams.
|
|
|
|
|
|
|
|
### Event: 'disconnect'
|
|
|
|
|
|
|
|
This event is emitted after calling the `.disconnect()` method in the parent
|
|
|
|
or in the child. After disconnecting it is no longer possible to send messages,
|
|
|
|
and the `.connected` property is false.
|
|
|
|
|
2013-03-26 16:34:42 +01:00
|
|
|
### Event: 'error'
|
|
|
|
|
|
|
|
* `err` {Error Object} the error.
|
|
|
|
|
|
|
|
Emitted when:
|
|
|
|
|
|
|
|
1. The process could not be spawned, or
|
|
|
|
2. The process could not be killed, or
|
2015-12-08 04:31:38 +01:00
|
|
|
3. Sending a message to the child process failed.
|
2013-03-26 16:34:42 +01:00
|
|
|
|
2015-12-08 04:31:38 +01:00
|
|
|
Note that the `'exit'` event may or may not fire after an error has occurred.
|
|
|
|
If you are listening on both events to fire a function, remember to guard
|
|
|
|
against calling your function twice.
|
2013-11-21 16:39:23 +01:00
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
See also [`ChildProcess#kill()`][] and [`ChildProcess#send()`][].
|
2013-03-26 16:34:42 +01:00
|
|
|
|
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.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Also, note that Node.js establishes signal handlers for `SIGINT` and
|
2015-12-08 04:31:38 +01:00
|
|
|
`SIGTERM`. It will not terminate due to receipt of those signals. It will exit.
|
2013-07-26 02:35:21 +02:00
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
See `waitpid(2)`.
|
|
|
|
|
2012-04-12 09:18:12 +02:00
|
|
|
### Event: 'message'
|
|
|
|
|
2015-06-15 04:06:22 +02:00
|
|
|
* `message` {Object} a parsed JSON object or primitive value.
|
2015-11-28 00:30:32 +01:00
|
|
|
* `sendHandle` {Handle object} a [`net.Socket`][] or [`net.Server`][] object, or
|
2015-06-15 04:06:22 +02:00
|
|
|
undefined.
|
2012-04-12 09:18:12 +02:00
|
|
|
|
2015-08-15 00:23:25 +02:00
|
|
|
Messages sent by `.send(message, [sendHandle])` are obtained using the
|
2015-11-28 00:30:32 +01:00
|
|
|
`'message'` event.
|
2012-04-12 09:18:12 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child.connected
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
* {Boolean} Set to false after `.disconnect` is called
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
If `.connected` is false, it is no longer possible to send messages.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child.disconnect()
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
Close the IPC channel between parent and child, allowing the child to exit
|
|
|
|
gracefully once there are no other connections keeping it alive. After calling
|
|
|
|
this method the `.connected` flag will be set to `false` in both the parent and
|
|
|
|
child, and it is no longer possible to send messages.
|
2013-07-26 02:35:21 +02:00
|
|
|
|
2015-12-08 04:31:38 +01:00
|
|
|
The `'disconnect'` event will be emitted when there are no messages in the
|
|
|
|
process of being received, most likely immediately.
|
2013-07-26 02:35:21 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
Note that you can also call `process.disconnect()` in the child process when the
|
2015-11-28 00:30:32 +01:00
|
|
|
child process has any open IPC channels with the parent (i.e [`fork()`][]).
|
2013-07-26 02:35:21 +02:00
|
|
|
|
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.
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const grep = spawn('grep', ['ssh']);
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
grep.on('close', (code, signal) => {
|
|
|
|
console.log(
|
|
|
|
`child process terminated due to receipt of signal ${signal}`);
|
2012-02-27 20:04:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// send SIGHUP to process
|
|
|
|
grep.kill('SIGHUP');
|
|
|
|
|
2013-03-26 16:34:42 +01:00
|
|
|
May emit an `'error'` event when the signal cannot be delivered. Sending a
|
|
|
|
signal to a child process that has already exited is not an error but may
|
2015-12-08 04:31:38 +01:00
|
|
|
have unforeseen consequences. Specifically, if the process identifier (PID) has
|
|
|
|
been reassigned to another process, the signal will be delivered to that
|
|
|
|
process instead. What happens next is anyone's guess.
|
2013-03-26 16:34:42 +01:00
|
|
|
|
|
|
|
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.
|
2012-02-27 20:04:08 +01:00
|
|
|
|
|
|
|
See `kill(2)`
|
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child.pid
|
|
|
|
|
|
|
|
* {Integer}
|
|
|
|
|
2015-12-08 04:31:38 +01:00
|
|
|
The process identifier (PID) of the child process.
|
2015-11-04 16:50:18 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const grep = spawn('grep', ['ssh']);
|
2015-11-04 16:50:18 +01:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
console.log(`Spawned child pid: ${grep.pid}`);
|
2015-11-04 16:50:18 +01:00
|
|
|
grep.stdin.end();
|
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
### child.send(message[, sendHandle][, callback])
|
2012-02-27 20:04:08 +01:00
|
|
|
|
|
|
|
* `message` {Object}
|
|
|
|
* `sendHandle` {Handle object}
|
2015-08-31 00:49:34 +02:00
|
|
|
* `callback` {Function}
|
|
|
|
* Return: Boolean
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
When using [`child_process.fork()`][] you can write to the child using
|
2015-08-31 00:49:34 +02:00
|
|
|
`child.send(message[, sendHandle][, callback])` and messages are received by
|
2012-04-12 09:18:12 +02:00
|
|
|
a `'message'` event on the child.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const cp = require('child_process');
|
|
|
|
const n = cp.fork(`${__dirname}/sub.js`);
|
2012-04-12 09:18:12 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
n.on('message', (m) => {
|
2012-04-12 09:18:12 +02:00
|
|
|
console.log('PARENT got message:', m);
|
|
|
|
});
|
|
|
|
|
|
|
|
n.send({ hello: 'world' });
|
|
|
|
|
|
|
|
And then the child script, `'sub.js'` might look like this:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
process.on('message', (m) => {
|
2012-04-12 09:18:12 +02:00
|
|
|
console.log('CHILD got message:', m);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.send({ foo: 'bar' });
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2015-12-08 04:31:38 +01:00
|
|
|
In the child, the `process` object will have a `send()` method, and `process`
|
2012-04-12 09:18:12 +02:00
|
|
|
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
|
2015-11-28 00:30:32 +01:00
|
|
|
the `'message'` event, since they are internal messages used by Node.js core.
|
|
|
|
Messages containing the prefix are emitted in the `'internalMessage'` event.
|
2015-06-04 22:26:36 +02:00
|
|
|
Avoid using this feature; it is subject to change without notice.
|
2012-04-12 09:18:12 +02:00
|
|
|
|
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
|
2015-11-28 00:30:32 +01:00
|
|
|
second argument to the `'message'` event.
|
2012-04-12 09:23:07 +02:00
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
The `callback` option is a function that is invoked after the message is
|
|
|
|
sent but before the target may have received it. It is called with a single
|
2015-11-28 00:30:32 +01:00
|
|
|
argument: `null` on success, or an [`Error`][] object on failure.
|
2015-08-31 00:49:34 +02:00
|
|
|
|
|
|
|
`child.send()` emits an `'error'` event if no callback was given and the message
|
|
|
|
cannot be sent, for example because the child process has already exited.
|
|
|
|
|
2015-10-25 19:48:41 +01:00
|
|
|
`child.send()` returns `false` if the channel has closed or when the backlog of
|
2015-08-31 00:49:34 +02:00
|
|
|
unsent messages exceeds a threshold that makes it unwise to send more.
|
2015-10-25 19:48:41 +01:00
|
|
|
Otherwise, it returns `true`. Use the callback mechanism to implement flow
|
|
|
|
control.
|
2013-03-26 16:34:42 +01:00
|
|
|
|
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:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const child = require('child_process').fork('child.js');
|
2012-04-12 09:23:07 +02:00
|
|
|
|
|
|
|
// Open up the server object and send the handle.
|
2015-12-15 00:20:25 +01:00
|
|
|
const server = require('net').createServer();
|
|
|
|
server.on('connection', (socket) => {
|
2012-04-12 09:23:07 +02:00
|
|
|
socket.end('handled by parent');
|
|
|
|
});
|
2015-12-15 00:20:25 +01:00
|
|
|
server.listen(1337, () => {
|
2012-04-12 09:23:07 +02:00
|
|
|
child.send('server', server);
|
|
|
|
});
|
|
|
|
|
2015-06-24 05:42:49 +02:00
|
|
|
And the child would then receive the server object as:
|
2012-04-12 09:23:07 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
process.on('message', (m, server) => {
|
2012-04-12 09:23:07 +02:00
|
|
|
if (m === 'server') {
|
2015-12-15 00:20:25 +01:00
|
|
|
server.on('connection', (socket) => {
|
2012-04-12 09:23:07 +02:00
|
|
|
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-02-27 19:31:24 +01:00
|
|
|
For `dgram` servers the workflow is exactly the same. Here you listen on
|
2015-11-28 00:30:32 +01:00
|
|
|
a `'message'` event instead of `'connection'` and use `server.bind` instead of
|
2013-05-29 16:35:00 +02:00
|
|
|
`server.listen`. (Currently only supported on UNIX platforms.)
|
2013-02-27 19:31:24 +01:00
|
|
|
|
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
|
2015-08-13 18:14:34 +02:00
|
|
|
socket to a "special" child process. Other sockets will go to a "normal"
|
|
|
|
process.
|
2012-04-12 09:23:07 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const normal = require('child_process').fork('child.js', ['normal']);
|
|
|
|
const special = require('child_process').fork('child.js', ['special']);
|
2012-04-12 09:23:07 +02:00
|
|
|
|
|
|
|
// Open up the server and send sockets to child
|
2015-12-15 00:20:25 +01:00
|
|
|
const server = require('net').createServer();
|
|
|
|
server.on('connection', (socket) => {
|
2012-04-12 09:23:07 +02:00
|
|
|
|
|
|
|
// if this is a VIP
|
|
|
|
if (socket.remoteAddress === '74.125.127.100') {
|
|
|
|
special.send('socket', socket);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-15 00:27:17 +02:00
|
|
|
// just the usual...
|
2012-04-12 09:23:07 +02:00
|
|
|
normal.send('socket', socket);
|
|
|
|
});
|
|
|
|
server.listen(1337);
|
|
|
|
|
|
|
|
The `child.js` could look like this:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
process.on('message', (m, socket) => {
|
2012-04-12 09:23:07 +02:00
|
|
|
if (m === 'socket') {
|
2015-12-15 00:20:25 +01: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
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child.stderr
|
2012-04-12 09:18:12 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
* {Stream object}
|
2013-07-26 02:35:21 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
A `Readable Stream` that represents the child process's `stderr`.
|
2013-07-26 02:35:21 +02:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
If the child was not spawned with `stdio[2]` set to `'pipe'`, then this will
|
|
|
|
not be set.
|
|
|
|
|
|
|
|
`child.stderr` is shorthand for `child.stdio[2]`. Both properties will refer
|
|
|
|
to the same object, or null.
|
|
|
|
|
|
|
|
### child.stdin
|
|
|
|
|
|
|
|
* {Stream object}
|
|
|
|
|
|
|
|
A `Writable Stream` that represents the child process's `stdin`.
|
|
|
|
If the child is waiting to read all its input, it will not continue until this
|
|
|
|
stream has been closed via `end()`.
|
|
|
|
|
|
|
|
If the child was not spawned with `stdio[0]` set to `'pipe'`, then this will
|
|
|
|
not be set.
|
|
|
|
|
|
|
|
`child.stdin` is shorthand for `child.stdio[0]`. Both properties will refer
|
|
|
|
to the same object, or null.
|
|
|
|
|
|
|
|
### child.stdio
|
|
|
|
|
|
|
|
* {Array}
|
|
|
|
|
|
|
|
A sparse array of pipes to the child process, corresponding with positions in
|
2015-11-28 00:30:32 +01:00
|
|
|
the [`stdio`][] option to [`spawn()`][] that have been set to `'pipe'`.
|
2015-11-04 16:50:18 +01:00
|
|
|
Note that streams 0-2 are also available as ChildProcess.stdin,
|
|
|
|
ChildProcess.stdout, and ChildProcess.stderr, respectively.
|
|
|
|
|
|
|
|
In the following example, only the child's fd `1` is setup as a pipe, so only
|
|
|
|
the parent's `child.stdio[1]` is a stream, all other values in the array are
|
|
|
|
`null`.
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const child_process = require('child_process');
|
2015-11-04 16:50:18 +01:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const child = child_process.spawn('ls', {
|
2015-11-04 16:50:18 +01:00
|
|
|
stdio: [
|
|
|
|
0, // use parents stdin for child
|
|
|
|
'pipe', // pipe child's stdout to parent
|
|
|
|
fs.openSync('err.out', 'w') // direct child's stderr to a file
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(child.stdio[0], null);
|
|
|
|
assert.equal(child.stdio[0], child.stdin);
|
|
|
|
|
|
|
|
assert(child.stdout);
|
|
|
|
assert.equal(child.stdio[1], child.stdout);
|
|
|
|
|
|
|
|
assert.equal(child.stdio[2], null);
|
|
|
|
assert.equal(child.stdio[2], child.stderr);
|
|
|
|
|
|
|
|
### child.stdout
|
|
|
|
|
|
|
|
* {Stream object}
|
|
|
|
|
|
|
|
A `Readable Stream` that represents the child process's `stdout`.
|
|
|
|
|
|
|
|
If the child was not spawned with `stdio[1]` set to `'pipe'`, then this will
|
|
|
|
not be set.
|
|
|
|
|
|
|
|
`child.stdout` is shorthand for `child.stdio[1]`. Both properties will refer
|
|
|
|
to the same object, or null.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-07-08 01:38:05 +02:00
|
|
|
## Asynchronous Process Creation
|
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
These methods follow the common async programming patterns (accepting a
|
|
|
|
callback or returning an EventEmitter).
|
|
|
|
|
|
|
|
### 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
|
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `encoding` {String} (Default: 'utf8')
|
|
|
|
* `shell` {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` {Number} (Default: 0)
|
|
|
|
* `maxBuffer` {Number} largest amount of data (in bytes) allowed on stdout or
|
|
|
|
stderr - if exceeded child process is killed (Default: `200*1024`)
|
|
|
|
* `killSignal` {String} (Default: 'SIGTERM')
|
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
|
|
|
* `callback` {Function} called with the output when process terminates
|
|
|
|
* `error` {Error}
|
|
|
|
* `stdout` {Buffer}
|
|
|
|
* `stderr` {Buffer}
|
|
|
|
* Return: ChildProcess object
|
|
|
|
|
|
|
|
Runs a command in a shell and buffers the output.
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const child = exec('cat *.js bad_file | wc -l',
|
|
|
|
(error, stdout, stderr) => {
|
|
|
|
console.log(`stdout: ${stdout}`);
|
|
|
|
console.log(`stderr: ${stderr}`);
|
2015-11-04 16:50:18 +01:00
|
|
|
if (error !== null) {
|
2015-12-15 00:20:25 +01:00
|
|
|
console.log(`exec error: ${error}`);
|
2015-11-04 16:50:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
The callback gets the arguments `(error, stdout, stderr)`. On success, `error`
|
2015-11-28 00:30:32 +01:00
|
|
|
will be `null`. On error, `error` will be an instance of [`Error`][] and `error.code`
|
2015-11-04 16:50:18 +01:00
|
|
|
will be the exit code of the child process, and `error.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 }
|
|
|
|
|
|
|
|
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 (in bytes) allowed on stdout or stderr - if this value is
|
|
|
|
exceeded then the child process is killed.
|
|
|
|
|
|
|
|
*Note: Unlike the `exec()` POSIX system call, `child_process.exec()` does not replace
|
|
|
|
the existing process and uses a shell to execute the command.*
|
|
|
|
|
|
|
|
### 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
|
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `encoding` {String} (Default: 'utf8')
|
|
|
|
* `timeout` {Number} (Default: 0)
|
|
|
|
* `maxBuffer` {Number} largest amount of data (in bytes) allowed on stdout or
|
|
|
|
stderr - if exceeded child process is killed (Default: 200\*1024)
|
|
|
|
* `killSignal` {String} (Default: 'SIGTERM')
|
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
|
|
|
* `callback` {Function} called with the output when process terminates
|
|
|
|
* `error` {Error}
|
|
|
|
* `stdout` {Buffer}
|
|
|
|
* `stderr` {Buffer}
|
|
|
|
* Return: ChildProcess object
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
This is similar to [`child_process.exec()`][] except it does not execute a
|
2015-11-04 16:50:18 +01:00
|
|
|
subshell but rather the specified file directly. This makes it slightly
|
2015-11-14 04:21:49 +01:00
|
|
|
leaner than [`child_process.exec()`][]. It has the same options.
|
2015-11-04 16:50:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
### child_process.fork(modulePath[, args][, options])
|
|
|
|
|
|
|
|
* `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
|
|
|
|
* `execPath` {String} Executable used to create the child process
|
|
|
|
* `execArgv` {Array} List of string arguments passed to the executable
|
|
|
|
(Default: `process.execArgv`)
|
|
|
|
* `silent` {Boolean} If true, stdin, stdout, and stderr of the child will be
|
|
|
|
piped to the parent, otherwise they will be inherited from the parent, see
|
2015-11-28 00:30:32 +01:00
|
|
|
the `'pipe'` and `'inherit'` options for [`spawn()`][]'s [`stdio`][] for more details
|
2015-11-04 16:50:18 +01:00
|
|
|
(default is false)
|
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
|
|
|
* Return: ChildProcess object
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
This is a special case of the [`child_process.spawn()`][] functionality for
|
|
|
|
spawning Node.js processes. In addition to having all the methods in a normal
|
|
|
|
ChildProcess instance, the returned object has a communication channel built-in.
|
2015-12-01 09:10:12 +01:00
|
|
|
See [`ChildProcess#send()`][] for details.
|
2015-11-04 16:50:18 +01:00
|
|
|
|
|
|
|
These child Node.js processes are still whole new instances of V8. Assume at
|
|
|
|
least 30ms startup and 10mb memory for each new Node.js. That is, you cannot
|
|
|
|
create many thousands of them.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
*Note: Unlike the `fork()` POSIX system call, [`child_process.fork()`][] does not clone the
|
2015-11-04 16:50:18 +01:00
|
|
|
current process.*
|
2014-07-08 01:38:05 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
### child_process.spawn(command[, args][, options])
|
2012-02-27 20:04:08 +01:00
|
|
|
|
|
|
|
* `command` {String} The command to run
|
|
|
|
* `args` {Array} List of string arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
|
|
|
* `env` {Object} Environment key-value pairs
|
2015-01-10 21:40:02 +01:00
|
|
|
* `stdio` {Array|String} Child's stdio configuration. (See
|
|
|
|
[below](#child_process_options_stdio))
|
2015-09-13 14:37:14 +02:00
|
|
|
* `detached` {Boolean} Prepare child to run independently of its parent
|
|
|
|
process. Specific behavior depends on the platform, see
|
2015-01-10 21:40:02 +01:00
|
|
|
[below](#child_process_options_detached))
|
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
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Launches a new process with the given `command`, with command line arguments in
|
|
|
|
`args`. If omitted, `args` defaults to an empty Array.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-07-08 01:38:05 +02:00
|
|
|
The third argument is used to specify additional options, with these defaults:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
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
|
|
|
|
2015-01-10 21:40:02 +01:00
|
|
|
Use `cwd` to specify the working directory from which the process is spawned.
|
|
|
|
If not given, the default is to inherit the current working directory.
|
|
|
|
|
|
|
|
Use `env` to specify environment variables that will be visible to the new
|
|
|
|
process, the default is `process.env`.
|
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:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const ls = spawn('ls', ['-lh', '/usr']);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ls.stdout.on('data', (data) => {
|
|
|
|
console.log(`stdout: ${data}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ls.stderr.on('data', (data) => {
|
|
|
|
console.log(`stderr: ${data}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ls.on('close', (code) => {
|
|
|
|
console.log(`child process exited with code ${code}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Example: A very elaborate way to run 'ps ax | grep ssh'
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const ps = spawn('ps', ['ax']);
|
|
|
|
const grep = spawn('grep', ['ssh']);
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ps.stdout.on('data', (data) => {
|
2010-10-28 14:18:16 +02:00
|
|
|
grep.stdin.write(data);
|
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ps.stderr.on('data', (data) => {
|
|
|
|
console.log(`ps stderr: ${data}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
ps.on('close', (code) => {
|
2010-10-28 14:18:16 +02:00
|
|
|
if (code !== 0) {
|
2015-12-15 00:20:25 +01:00
|
|
|
console.log(`ps process exited with code ${code}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
}
|
|
|
|
grep.stdin.end();
|
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
grep.stdout.on('data', (data) => {
|
|
|
|
console.log(`${data}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
grep.stderr.on('data', (data) => {
|
|
|
|
console.log(`grep stderr: ${data}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
grep.on('close', (code) => {
|
2010-10-28 14:18:16 +02:00
|
|
|
if (code !== 0) {
|
2015-12-15 00:20:25 +01:00
|
|
|
console.log(`grep process exited with code ${code}`);
|
2010-10-28 14:18:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Example of checking for failed exec:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const child = spawn('bad_command');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
child.on('error', (err) => {
|
2013-08-14 21:31:33 +02:00
|
|
|
console.log('Failed to start child process.');
|
2010-10-28 14:18:16 +02:00
|
|
|
});
|
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
#### options.detached
|
|
|
|
|
|
|
|
On Windows, this makes it possible for the child to continue running after the
|
|
|
|
parent exits. The child will have a new console window (this cannot be
|
|
|
|
disabled).
|
|
|
|
|
|
|
|
On non-Windows, if the `detached` option is set, the child process will be made
|
|
|
|
the leader of a new process group and session. Note that child processes may
|
|
|
|
continue running after the parent exits whether they are detached or not. See
|
|
|
|
`setsid(2)` for more information.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const out = fs.openSync('./out.log', 'a');
|
|
|
|
const err = fs.openSync('./out.log', 'a');
|
2015-11-04 16:50:18 +01:00
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const child = spawn('prg', [], {
|
2015-11-04 16:50:18 +01:00
|
|
|
detached: true,
|
|
|
|
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 after the parent exits 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.
|
|
|
|
|
2015-03-03 06:10:19 +01:00
|
|
|
#### options.stdio
|
2015-01-10 21:40:02 +01:00
|
|
|
|
|
|
|
As a shorthand, the `stdio` argument may be one of the following strings:
|
|
|
|
|
|
|
|
* `'pipe'` - `['pipe', 'pipe', 'pipe']`, this is the default value
|
|
|
|
* `'ignore'` - `['ignore', 'ignore', 'ignore']`
|
|
|
|
* `'inherit'` - `[process.stdin, process.stdout, process.stderr]` or `[0,1,2]`
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Otherwise, the `'stdio'` option to [`child_process.spawn()`][] is an array where each
|
2012-05-16 18:04:24 +02:00
|
|
|
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
|
2015-08-13 18:14:34 +02:00
|
|
|
trigger ChildProcess.on('message'). If the child is an Node.js program, then
|
2012-06-04 14:04:15 +02:00
|
|
|
the presence of an IPC channel will enable process.send() and
|
|
|
|
process.on('message').
|
2015-08-13 18:14:34 +02:00
|
|
|
3. `'ignore'` - Do not set this file descriptor in the child. Note that Node.js
|
2012-06-04 14:04:15 +02:00
|
|
|
will always open fd 0 - 2 for the processes it spawns. When any of these is
|
2015-08-13 18:14:34 +02:00
|
|
|
ignored Node.js will open `/dev/null` and attach it to the child's fd.
|
2012-06-04 14:04:15 +02:00
|
|
|
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
|
2015-08-13 18:14:34 +02:00
|
|
|
file descriptor is duplicated in the child process to the fd that
|
doc: streams must be open to be passed to child
spawn stdio options can be a 'stream', but the following code
fails with "Incorrect value for stdio stream: [object Object]",
despite being a stream. The problem is the test isn't really
for a stream, its for an object with a numeric `.fd` property,
and streams do not have an fd until their async 'open' event
has occurred. This is reasonable, but was not documented.
child_process.spawn('date', [], {stdio: [
'ignore',
fs.createWriteStream('out.txt',{flags:'a'}),
'ignore']})
2014-01-09 02:16:17 +01:00
|
|
|
corresponds to the index in the `stdio` array. Note that the stream must
|
|
|
|
have an underlying descriptor (file streams do not until the `'open'`
|
|
|
|
event has occurred).
|
2015-08-13 18:14:34 +02:00
|
|
|
5. Positive integer - The integer value is interpreted as a file descriptor
|
2012-06-04 14:04:15 +02:00
|
|
|
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
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-12-15 00:20:25 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
2012-05-16 18:04:24 +02:00
|
|
|
|
|
|
|
// 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'] });
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
See also: [`child_process.exec()`][] and [`child_process.fork()`][]
|
2012-02-27 20:04:08 +01:00
|
|
|
|
2014-07-08 01:38:05 +02:00
|
|
|
## Synchronous Process Creation
|
|
|
|
|
|
|
|
These methods are **synchronous**, meaning they **WILL** block the event loop,
|
|
|
|
pausing execution of your code until the spawned process exits.
|
|
|
|
|
|
|
|
Blocking calls like these are mostly useful for simplifying general purpose
|
|
|
|
scripting tasks and for simplifying the loading/processing of application
|
|
|
|
configuration at startup.
|
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child_process.execFileSync(file[, args][, options])
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
* `file` {String} The filename of the program to run
|
2014-02-10 21:40:48 +01:00
|
|
|
* `args` {Array} List of string arguments
|
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
|
|
|
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
|
|
|
|
- supplying this value will override `stdio[0]`
|
2015-11-04 16:50:18 +01:00
|
|
|
* `stdio` {Array} Child's stdio configuration. (Default: 'pipe')
|
|
|
|
- `stderr` by default will be output to the parent process' stderr unless
|
|
|
|
`stdio` is specified
|
2014-02-10 21:40:48 +01:00
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
|
|
|
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
|
|
|
|
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
|
2014-12-26 11:41:30 +01:00
|
|
|
* `maxBuffer` {Number} largest amount of data (in bytes) allowed on stdout or
|
|
|
|
stderr - if exceeded child process is killed
|
2014-02-10 21:40:48 +01:00
|
|
|
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
|
2015-11-04 16:50:18 +01:00
|
|
|
* return: {Buffer|String} The stdout from the command
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
`execFileSync` will not return until the child process has fully closed. When a
|
2014-02-10 21:40:48 +01:00
|
|
|
timeout has been encountered and `killSignal` is sent, the method won't return
|
|
|
|
until the process has completely exited. That is to say, if the process handles
|
|
|
|
the `SIGTERM` signal and doesn't exit, your process will wait until the child
|
|
|
|
process has exited.
|
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
If the process times out, or has a non-zero exit code, this method ***will***
|
2015-11-28 00:30:32 +01:00
|
|
|
throw. The [`Error`][] object will contain the entire result from
|
2015-11-14 04:21:49 +01:00
|
|
|
[`child_process.spawnSync()`][]
|
2015-11-04 16:50:18 +01:00
|
|
|
|
2015-09-22 21:01:23 +02:00
|
|
|
### child_process.execSync(command[, options])
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-09-22 21:01:23 +02:00
|
|
|
* `command` {String} The command to run
|
2014-02-10 21:40:48 +01:00
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
|
|
|
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
|
|
|
|
- supplying this value will override `stdio[0]`
|
2014-02-18 01:29:23 +01:00
|
|
|
* `stdio` {Array} Child's stdio configuration. (Default: 'pipe')
|
|
|
|
- `stderr` by default will be output to the parent process' stderr unless
|
|
|
|
`stdio` is specified
|
2014-02-10 21:40:48 +01:00
|
|
|
* `env` {Object} Environment key-value pairs
|
2015-10-19 21:10:18 +02:00
|
|
|
* `shell` {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`.)
|
2014-02-10 21:40:48 +01: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).)
|
|
|
|
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
|
|
|
|
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
|
2014-12-26 11:41:30 +01:00
|
|
|
* `maxBuffer` {Number} largest amount of data (in bytes) allowed on stdout or
|
|
|
|
stderr - if exceeded child process is killed
|
2014-02-10 21:40:48 +01:00
|
|
|
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
|
|
|
|
* return: {Buffer|String} The stdout from the command
|
|
|
|
|
2015-09-22 21:01:23 +02:00
|
|
|
`execSync` will not return until the child process has fully closed. When a
|
2014-02-10 21:40:48 +01:00
|
|
|
timeout has been encountered and `killSignal` is sent, the method won't return
|
|
|
|
until the process has completely exited. That is to say, if the process handles
|
|
|
|
the `SIGTERM` signal and doesn't exit, your process will wait until the child
|
|
|
|
process has exited.
|
|
|
|
|
|
|
|
If the process times out, or has a non-zero exit code, this method ***will***
|
2015-11-28 00:30:32 +01:00
|
|
|
throw. The [`Error`][] object will contain the entire result from
|
2015-11-14 04:21:49 +01:00
|
|
|
[`child_process.spawnSync()`][]
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
### child_process.spawnSync(command[, args][, options])
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
* `command` {String} The command to run
|
2015-09-22 21:01:23 +02:00
|
|
|
* `args` {Array} List of string arguments
|
2014-02-10 21:40:48 +01:00
|
|
|
* `options` {Object}
|
|
|
|
* `cwd` {String} Current working directory of the child process
|
|
|
|
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
|
|
|
|
- supplying this value will override `stdio[0]`
|
2015-11-04 16:50:18 +01:00
|
|
|
* `stdio` {Array} Child's stdio configuration.
|
2014-02-10 21:40:48 +01:00
|
|
|
* `env` {Object} Environment key-value pairs
|
|
|
|
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
|
|
|
|
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
|
|
|
|
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
|
2014-12-26 11:41:30 +01:00
|
|
|
* `maxBuffer` {Number} largest amount of data (in bytes) allowed on stdout or
|
|
|
|
stderr - if exceeded child process is killed
|
2014-02-10 21:40:48 +01:00
|
|
|
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
|
2015-11-04 16:50:18 +01:00
|
|
|
* return: {Object}
|
|
|
|
* `pid` {Number} Pid of the child process
|
|
|
|
* `output` {Array} Array of results from stdio output
|
|
|
|
* `stdout` {Buffer|String} The contents of `output[1]`
|
|
|
|
* `stderr` {Buffer|String} The contents of `output[2]`
|
|
|
|
* `status` {Number} The exit code of the child process
|
|
|
|
* `signal` {String} The signal used to kill the child process
|
|
|
|
* `error` {Error} The error object if the child process failed or timed out
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-04 16:50:18 +01:00
|
|
|
`spawnSync` will not return until the child process has fully closed. When a
|
2014-02-10 21:40:48 +01:00
|
|
|
timeout has been encountered and `killSignal` is sent, the method won't return
|
|
|
|
until the process has completely exited. That is to say, if the process handles
|
|
|
|
the `SIGTERM` signal and doesn't exit, your process will wait until the child
|
|
|
|
process has exited.
|
2015-11-14 04:21:49 +01:00
|
|
|
|
|
|
|
[`child_process.exec()`]: #child_process_child_process_exec_command_options_callback
|
2015-11-28 00:30:32 +01:00
|
|
|
[`child_process.fork()`]: #child_process_child_process_fork_modulepath_args_options
|
2015-11-14 04:21:49 +01:00
|
|
|
[`child_process.spawn()`]: #child_process_child_process_spawn_command_args_options
|
|
|
|
[`child_process.spawnSync()`]: #child_process_child_process_spawnsync_command_args_options
|
2015-11-28 00:30:32 +01:00
|
|
|
[`ChildProcess#kill()`]: #child_process_child_kill_signal
|
|
|
|
[`ChildProcess#send()`]: #child_process_child_send_message_sendhandle_callback
|
|
|
|
[`Error`]: errors.html#errors_class_error
|
|
|
|
[`EventEmitter`]: events.html#events_class_events_eventemitter
|
|
|
|
[`exec()`]: #child_process_child_process_exec_command_options_callback
|
|
|
|
[`execFile()`]: #child_process_child_process_execfile_file_args_options_callback
|
|
|
|
[`fork()`]: #child_process_child_process_fork_modulepath_args_options
|
|
|
|
[`net.Server`]: net.html#net_class_net_server
|
|
|
|
[`net.Socket`]: net.html#net_class_net_socket
|
|
|
|
[`spawn()`]: #child_process_child_process_spawn_command_args_options
|
|
|
|
[`stdio`]: #child_process_options_stdio
|
|
|
|
[below]: #child_process_asynchronous_process_creation
|
|
|
|
[synchronous counterparts]: #child_process_synchronous_process_creation
|