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


  • command <string> 运行的命令。
  • args <string[]> 参数列表。
  • options <Object>

    • cwd <string> 子进程的当前工作目录。
    • input <string> | <Buffer> | <TypedArray> | <DataView> 传入衍生进程的 stdin。指定该值会覆盖 stdio[0]
    • argv0 <string> 发送给子进程的 argv[0] 的值。如果没有指定,则设为 command 的值。
    • stdio <string> | <Array> 子进程的 stdio 配置。
    • env <Object> 环境变量键值对。
    • uid <number> 进程的用户标识。参阅 setuid(2)
    • gid <number> 进程的群组标识。参阅 setgid(2)
    • timeout <number> 允许进程运行的最长时间,以毫秒为单位。默认为 undefined
    • killSignal <string> | <integer> 用于杀死衍生进程的信号。默认为 'SIGTERM'
    • maxBuffer <number> stdout 或 stderr 允许的最大字节数。如果超过限制,则子进程会终止。参阅 maxBuffer与Unicode。默认为 200*1024
    • encoding <string> 用于 stdio 输入和输出的字符编码。默认为 'buffer'
    • shell <boolean> | <string> 如果设为 true,则在 shell 中运行 command。 在 UNIX 上使用 '/bin/sh',在 Windows 上使用 process.env.ComSpec。 传入字符串则指定其他 shell。 参阅 Shell的要求Windows默认的Shell。 默认为 false(没有 shell)。
    • windowsVerbatimArguments <boolean> 在 Windows 上是否为参数加引号或转义。如果指定了 shell,则自动设为 true。默认为 false
    • windowsHide <boolean> 是否隐藏子进程的控制台窗口。默认为 false
  • 返回: <Object>

child_process.spawn() 的区别是,该方法需等到子进程完全关闭后才返回。 当发生超时且已发送 killSignal 时,该方法也需等到进程完全退出后才返回。 如果子进程拦截并处理了 SIGTERM 信号且没有退出,则父进程会一直等待直到子进程退出。

  • command <string> The command to run.
  • args <string[]> List of string arguments.
  • options <Object>

    • cwd <string> 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].
    • 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's stdio configuration.
    • env <Object> Environment key-value pairs.
    • 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. See caveat at maxBuffer and Unicode. Default: 200 * 1024.
    • encoding <string> The encoding used for all stdio inputs and outputs. Default: 'buffer'.
    • 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> No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified. Default: false.
    • windowsHide <boolean> Hide the subprocess console window that would normally be created on Windows systems. Default: false.
  • Returns: <Object>

    • pid <number> Pid of the child process.
    • output <Array> Array of results from stdio output.
    • stdout <Buffer> | <string> The contents of output[1].
    • stderr <Buffer> | <string> The contents of output[2].
    • status <number> The exit code of the child process.
    • signal <string> The signal used to kill the child process.
    • error <Error> The error object if the child process failed or timed out.

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. Note that if the process intercepts and handles the SIGTERM signal and doesn't exit, the parent process will wait until the child process has exited.

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.