child_process.spawnSync(command[, args][, options])
command<string> 要运行的命令。args<string[]> 字符串参数列表。options<Object>cwd<string> | <URL> 子进程的当前工作目录。input<string> | <Buffer> | <TypedArray> | <DataView> 将作为标准输入传递给生成的进程的值。如果stdio[0]设置为'pipe',提供此值将覆盖stdio[0]。argv0<string> 明确设置发送给子进程的argv[0]的值。如果未指定,将设置为command。stdio<string> | <Array> 子进程的 stdio 配置。参见child_process.spawn()的stdio。默认值:'pipe'。env<Object> 环境键值对。默认值:process.env。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'。shell<boolean> | <string> 如果为true,将在 shell 中运行command。在 Unix 上使用'/bin/sh',在 Windows 上使用process.env.ComSpec。可以通过字符串指定不同的 shell。参见 Shell 要求 和 默认 Windows shell。默认值:false(不使用 shell)。windowsVerbatimArguments<boolean> 在 Windows 上不会对参数进行引号或转义处理。在 Unix 上会被忽略。当shell被指定为 CMD 时,该选项会自动设置为true。默认值:false。windowsHide<boolean> 隐藏通常在 Windows 系统上创建的子进程控制台窗口。默认值:false。
- 返回:<Object>
child_process.spawnSync() 方法通常与 child_process.spawn() 相同,唯一的区别是该函数在子进程完全关闭之前不会返回。当遇到超时并发送 killSignal 时,该方法也不会返回,直到进程完全退出。如果进程拦截并处理了 SIGTERM 信号且没有退出,父进程将等待直到子进程退出。
【The child_process.spawnSync() method is generally identical to
child_process.spawn() with the exception that the function 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 process intercepts and handles the SIGTERM signal
and doesn't exit, the parent process will wait until the child process has
exited.】
如果启用了 shell 选项,请不要将未经过滤的用户输入传递给此函数。任何包含 shell 元字符的输入都可能被用来触发任意命令的执行。