child_process.spawnSync(command[, args][, options])


  • command <string> 要运行的命令。

    ¥command <string> The command to run.

  • args <string[]> 字符串参数列表。

    ¥args <string[]> List of string arguments.

  • options <Object>

    • cwd <string> | <URL> 子进程的当前工作目录。

      ¥cwd <string> | <URL> Current working directory of the child process.

    • input <string> | <Buffer> | <TypedArray> | <DataView> 将作为标准输入传给衍生进程的值。如果 stdio[0] 设置为 'pipe',则提供该值将覆盖 stdio[0]

      ¥input <string> | <Buffer> | <TypedArray> | <DataView> The value which will be passed as stdin to the spawned process. If stdio[0] is set to 'pipe', Supplying this value will override stdio[0].

    • argv0 <string> 显式设置发送给子进程的 argv[0] 的值。如果未指定,这将设置为 command

      ¥argv0 <string> Explicitly set the value of argv[0] sent to the child process. This will be set to command if not specified.

    • stdio <string> | <Array> 子进程的标准输入输出配置。参见 child_process.spawn()stdio。默认值:'pipe'

      ¥stdio <string> | <Array> Child's stdio configuration. See child_process.spawn()'s stdio. Default: 'pipe'.

    • env <Object> 环境变量键值对。默认值:process.env

      ¥env <Object> Environment key-value pairs. Default: process.env.

    • uid <number> 设置进程的用户身份(请参阅 setuid(2))。

      ¥uid <number> Sets the user identity of the process (see setuid(2)).

    • gid <number> 设置进程的组标识(请参阅 setgid(2))。

      ¥gid <number> Sets the group identity of the process (see setgid(2)).

    • timeout <number> 允许进程运行的最长时间(以毫秒为单位)。默认值:undefined

      ¥timeout <number> In milliseconds the maximum amount of time the process is allowed to run. Default: undefined.

    • killSignal <string> | <integer> 衍生的进程将被终止时要使用的信号值。默认值:'SIGTERM'

      ¥killSignal <string> | <integer> The signal value to be used when the spawned process will be killed. Default: 'SIGTERM'.

    • maxBuffer <number> 标准输出或标准错误上允许的最大数据量(以字节为单位)。如果超过,则子进程将终止并截断任何输出。请参阅 maxBuffer 和 Unicode 的警告。默认值:1024 * 1024

      ¥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> 用于所有标准输入输出的输入和输出的编码。默认值:'buffer'

      ¥encoding <string> The encoding used for all stdio inputs and outputs. Default: 'buffer'.

    • shell <boolean> | <string> 如果是 true,则在 shell 内运行 command。在 Unix 上使用 '/bin/sh',在 Windows 上使用 process.env.ComSpec。可以将不同的 shell 指定为字符串。参见 Shell 要求默认 Windows shell。默认值:false(无壳)。

      ¥shell <boolean> | <string> If true, runs command inside of a shell. Uses '/bin/sh' on Unix, and process.env.ComSpec on Windows. A different shell can be specified as a string. See Shell requirements and Default Windows shell. Default: false (no shell).

    • windowsVerbatimArguments <boolean> 在 Windows 上不为参数加上引号或转义。在 Unix 上被忽略。当指定了 shell 并且是 CMD 时,则自动设置为 true。默认值:false

      ¥windowsVerbatimArguments <boolean> No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. Default: false.

    • windowsHide <boolean> 隐藏通常在 Windows 系统上创建的子进程控制台窗口。默认值:false

      ¥windowsHide <boolean> Hide the subprocess console window that would normally be created on Windows systems. Default: false.

  • 返回:<Object>

    ¥Returns: <Object>

    • pid <number> 子进程的 pid。

      ¥pid <number> Pid of the child process.

    • output <Array> 来自标准输入输出的输出的结果数组。

      ¥output <Array> Array of results from stdio output.

    • stdout <Buffer> | <string> output[1] 的内容。

      ¥stdout <Buffer> | <string> The contents of output[1].

    • stderr <Buffer> | <string> output[2] 的内容。

      ¥stderr <Buffer> | <string> The contents of output[2].

    • status <number> | <null> 子进程的退出码,如果子进程因信号而终止,则为 null

      ¥status <number> | <null> The exit code of the subprocess, or null if the subprocess terminated due to a signal.

    • signal <string> | <null> 用于终止子进程的信号,如果子进程没有因信号而终止,则为 null

      ¥signal <string> | <null> The signal used to kill the subprocess, or null if the subprocess did not terminate due to a signal.

    • error <Error> 如果子进程失败或超时,则为错误对象。

      ¥error <Error> The error object if the child process failed or timed out.

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 元字符的输入都可用于触发任意命令执行。

¥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.