child_process.execSync(command[, options])


  • command <string> 要运行的命令。
  • options <Object>
    • cwd <string> | <URL> 子进程的当前工作目录。
    • input <string> | <Buffer> | <TypedArray> | <DataView> 将作为标准输入传给衍生进程的值。 提供此值将覆盖 stdio[0]
    • stdio <string> | <Array> 子进程的标准输入输出配置。 除非指定 stdio,否则默认情况下 stderr 将输出到父进程的标准错误。 默认值: 'pipe'
    • env <Object> 环境变量键值对。 默认值: process.env
    • shell <string> 用于执行命令的 shell。 请参阅 shell 的要求默认的 Windows shell默认值: Unix 上是 '/bin/sh',Windows 上是 process.env.ComSpec
    • uid <number> 设置进程的用户标识。 (参见 setuid(2))。
    • gid <number> 设置进程的群组标识。 (参见 setgid(2))。
    • timeout <number> 允许进程运行的最长时间(以毫秒为单位)。 默认值: undefined
    • killSignal <string> | <integer> 衍生的进程将被终止时要使用的信号值。 默认值: 'SIGTERM'
    • maxBuffer <number> 标准输出或标准错误上允许的最大数据量(以字节为单位)。 如果超过,则子进程将终止并截断任何输出。 请参阅 maxBuffer 和 Unicode 的注意事项。 默认值: 1024 * 1024
    • encoding <string> 用于所有标准输入输出的输入和输出的编码。 默认值: 'buffer'
    • windowsHide <boolean> 隐藏通常在 Windows 系统上创建的子进程控制台窗口。 默认值: false
  • 返回: <Buffer> | <string> 命令的标准输出。

child_process.execSync() 方法通常与 child_process.exec() 相同,不同之处在于该方法在子进程完全关闭之前不会返回。 当遇到超时并发送 killSignal 时,该方法在进程完全退出之前不会返回。 如果子进程拦截并处理了 SIGTERM 信号且没有退出,则父进程会一直等到子进程退出。

如果进程超时或具有非零退出码,则此方法将抛出错误。 Error 对象将包含 child_process.spawnSync() 的整个结果。

切勿将未经处理的用户输入传给此函数。 任何包含 shell 元字符的输入都可用于触发任意命令执行。

  • command <string> The command to run.
  • options <Object>
    • cwd <string> | <URL> Current working directory of the child process.
    • input <string> | <Buffer> | <TypedArray> | <DataView> The value which will be passed as stdin to the spawned process. Supplying this value will override stdio[0].
    • stdio <string> | <Array> Child's stdio configuration. stderr by default will be output to the parent process' stderr unless stdio is specified. Default: 'pipe'.
    • env <Object> Environment key-value pairs. Default: process.env.
    • shell <string> Shell to execute the command with. See Shell requirements and Default Windows shell. Default: '/bin/sh' on Unix, process.env.ComSpec on Windows.
    • 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> | <integer> The signal value to be used when the spawned process will be killed. Default: 'SIGTERM'.
    • maxBuffer <number> Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is truncated. See caveat at maxBuffer and Unicode. Default: 1024 * 1024.
    • encoding <string> The encoding used for all stdio inputs and outputs. Default: 'buffer'.
    • windowsHide <boolean> Hide the subprocess console window that would normally be created on Windows systems. Default: false.
  • Returns: <Buffer> | <string> The stdout from the command.

The child_process.execSync() method is generally identical to child_process.exec() with the exception that the method will not return until the child process has fully closed. When a timeout has been encountered and killSignal is sent, the method won't return until the process has completely exited. If the child process intercepts and handles the SIGTERM signal and doesn't exit, the parent process will wait until the child process has exited.

If the process times out or has a non-zero exit code, this method will throw. The Error object will contain the entire result from child_process.spawnSync().

Never pass unsanitized user input to this function. Any input containing shell metacharacters may be used to trigger arbitrary command execution.