Skip to content

Commit e8df267

Browse files
committed
child_process: js bits for spawnSync/execSync
This implements the user-facing APIs that lets one run a child process and block until it exits. Logic shared with the async counterpart of each function was refactored to enable code reuse. Docs and tests are included.
1 parent fa4eb47 commit e8df267

6 files changed

+687
-80
lines changed

doc/api/child_process.markdown

+87
Original file line numberDiff line numberDiff line change
@@ -589,4 +589,91 @@ done with care and by default will talk over the fd represented an
589589
environmental variable `NODE_CHANNEL_FD` on the child process. The input and
590590
output on this fd is expected to be line delimited JSON objects.
591591

592+
## child_process.spawnSync(command, [args], [options])
593+
594+
* `command` {String} The command to run
595+
* `args` {Array} List of string arguments
596+
* `options` {Object}
597+
* `cwd` {String} Current working directory of the child process
598+
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
599+
- supplying this value will override `stdio[0]`
600+
* `stdio` {Array} Child's stdio configuration.
601+
* `env` {Object} Environment key-value pairs
602+
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
603+
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
604+
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
605+
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
606+
* `maxBuffer` {Number}
607+
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
608+
* return: {Object}
609+
* `pid` {Number} Pid of the child process
610+
* `output` {Array} Array of results from stdio output
611+
* `stdout` {Buffer|String} The contents of `output[1]`
612+
* `stderr` {Buffer|String} The contents of `output[2]`
613+
* `status` {Number} The exit code of the child process
614+
* `signal` {String} The signal used to kill the child process
615+
* `error` {Error} The error object if the child process failed or timedout
616+
617+
`spawnSync` will not return until the child process has fully closed. When a
618+
timeout has been encountered and `killSignal` is sent, the method won't return
619+
until the process has completely exited. That is to say, if the process handles
620+
the `SIGTERM` signal and doesn't exit, your process will wait until the child
621+
process has exited.
622+
623+
## child_process.execFileSync(command, [args], [options])
624+
625+
* `command` {String} The command to run
626+
* `args` {Array} List of string arguments
627+
* `options` {Object}
628+
* `cwd` {String} Current working directory of the child process
629+
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
630+
- supplying this value will override `stdio[0]`
631+
* `stdio` {Array} Child's stdio configuration.
632+
* `env` {Object} Environment key-value pairs
633+
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
634+
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
635+
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
636+
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
637+
* `maxBuffer` {Number}
638+
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
639+
* return: {Buffer|String} The stdout from the command
640+
641+
`execFileSync` will not return until the child process has fully closed. When a
642+
timeout has been encountered and `killSignal` is sent, the method won't return
643+
until the process has completely exited. That is to say, if the process handles
644+
the `SIGTERM` signal and doesn't exit, your process will wait until the child
645+
process has exited.
646+
647+
If the process times out, or has a non-zero exit code, this method ***will***
648+
throw. The `Error` object will contain the entire result from
649+
[`child_process.spawnSync`](#child_process_child_process_spawnsync_command_args_options)
650+
651+
652+
## child_process.execSync(command, [options])
653+
654+
* `command` {String} The command to run
655+
* `options` {Object}
656+
* `cwd` {String} Current working directory of the child process
657+
* `input` {String|Buffer} The value which will be passed as stdin to the spawned process
658+
- supplying this value will override `stdio[0]`
659+
* `stdio` {Array} Child's stdio configuration.
660+
* `env` {Object} Environment key-value pairs
661+
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
662+
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
663+
* `timeout` {Number} In milliseconds the maximum amount of time the process is allowed to run. (Default: undefined)
664+
* `killSignal` {String} The signal value to be used when the spawned process will be killed. (Default: 'SIGTERM')
665+
* `maxBuffer` {Number}
666+
* `encoding` {String} The encoding used for all stdio inputs and outputs. (Default: 'buffer')
667+
* return: {Buffer|String} The stdout from the command
668+
669+
`execSync` will not return until the child process has fully closed. When a
670+
timeout has been encountered and `killSignal` is sent, the method won't return
671+
until the process has completely exited. That is to say, if the process handles
672+
the `SIGTERM` signal and doesn't exit, your process will wait until the child
673+
process has exited.
674+
675+
If the process times out, or has a non-zero exit code, this method ***will***
676+
throw. The `Error` object will contain the entire result from
677+
[`child_process.spawnSync`](#child_process_child_process_spawnsync_command_args_options)
678+
592679
[EventEmitter]: events.html#events_class_events_eventemitter

0 commit comments

Comments
 (0)