child_process
exec
exec
Spawns a shell then executes the command
within that shell, buffering any generated output. The command
string passed to the exec function is processed directly by the shell, and special characters (vary based on shell) need to be dealt with accordingly:
Never pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution.
The stdout
and stderr
arguments passed to the stream will contain the stdout and stderr output of the child process. By default, Node.js will decode the output as UTF-8 and pass strings to the callback. The encoding
option can be used to specify the character encoding used to decode the stdout and stderr output. If encoding
is 'buffer'
, or an unrecognized character encoding, Buffer
objects will be passed to the callback instead.
If timeout
is greater than 0
, the parent will send the signal identified by the killSignal
property (the default is 'SIGTERM'
) if the child runs longer than timeout
milliseconds.
Unlike the exec(3)
POSIX system call, exec()
does not replace the existing process and uses a shell to execute the command.
execFile
execFile
The execFile()
function is similar to exec()
except that it does not spawn a shell by default. Rather, the specified executable file
is spawned directly as a new process making it slightly more efficient than exec()
.
The same options as exec()
are supported. Since a shell is not spawned, behaviors such as I/O redirection and file globbing are not supported.
The stdout
and stderr
arguments passed to the stream will contain the stdout and stderr output of the child process. By default, Node.js will decode the output as UTF-8 and pass strings to the callback. The encoding
option can be used to specify the character encoding used to decode the stdout and stderr output. If encoding
is 'buffer'
, or an unrecognized character encoding, Buffer
objects will be passed to the callback instead.
If the shell
option is enabled, do not pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution.
Last updated