child_process.execSync(command[, options])
command<string> 运行命令。options<Object>cwd<string> | <URL> 子进程的当前工作目录。input<string> | <Buffer> | <TypedArray> | <DataView> 将作为标准输入传递给生成的进程的值。提供此值将覆盖stdio[0]。stdio<string> | <Array> 子进程的标准输入输出配置。默认情况下,stderr将输出到父进程的标准错误,除非指定了stdio。默认值:'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> stdout 或 stderr 上允许的最大数据量(以字节为单位)。如果超出,子进程将被终止,且所有输出将被截断。参见maxBuffer和 Unicode 的注意事项。 默认值:1024 * 1024。encoding<string> 用于所有标准输入和输出的编码。 默认值:'buffer'。windowsHide<boolean> 隐藏通常会在 Windows 系统上创建的子进程控制台窗口。默认值:false。
- 返回:<Buffer> | <string> 命令的标准输出。
child_process.execSync() 方法通常与 child_process.exec() 相同,唯一的区别是该方法在子进程完全关闭之前不会返回。当遇到超时并发送 killSignal 时,该方法不会返回,直到进程完全退出。如果子进程拦截并处理了 SIGTERM 信号而没有退出,父进程将等待直到子进程退出。
🌐 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.
如果进程超时或退出代码非零,此方法将抛出异常。Error 对象将包含来自 child_process.spawnSync() 的完整结果。
🌐 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().
不要将未经清理的用户输入传递给此函数。任何包含 shell 元字符的输入都可能被用来触发任意命令执行。