pjs.spawn()
API Overview
The pjs.spawn() API is used to spawn a process and capture the output.
See the Node.js doc for child_process.spawn() for details on the "cmd", "args", and "options" parameters.
Parameters
- options: An object with the following properties:
- cmd (String) - The command to run.
- args (Array/optional) - Array of arguments to pass to the command.
- options (Object/optional) - Options object. See child_process.spawn() for details.
- input (String/optional) - Input to write to stdin of the spawned process.
Return Value
An object with the following properties:
- stdout (String): Data from the child processes's stdout. This property will only be present if output was received on stdout.
- stderr (String): Data from the child processes's stderr. This property will only be present if output was received on stderr.
- code (Number): Exit code, if the process ended on its own.
- signal (String): Signal name, if process ended due to signal.
Exception Handling
An Error instance will be thrown with exception details.
Examples
Run command: ls -la /tmp
try {
var output = pjs.spawn({
cmd: "ls",
args: [
"-la",
"/tmp"
]
});
console.log(output.stdout || output.stderr);
}
catch(error) {
console.log(error);
}Write data to command's stdin
try {
var output = pjs.spawn({
cmd: "cat",
input: "hello, world\n"
});
console.log(output.stdout || output.stderr);
}
catch(error) {
console.log(error);
}