child_process.execFileSync(file[, args][, options])
版本历史
| 版本 | 变更 |
|---|---|
| v16.4.0, v14.18.0 | 选项 |
| v10.10.0 | 选项 |
| v8.8.0 | 现在支持 |
| v8.0.0 | 选项 |
| v6.2.1, v4.5.0 | 选项 |
| v0.11.12 | 新增于: v0.11.12 |
-
file<string> 要运行的可执行文件的名称或路径。\
file<string> The name or path of the executable file 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. Ifstdio[0]is set to'pipe', Supplying this value will overridestdio[0]. -
stdio<string> | <Array> 子进程的标准输入输出配置。参见child_process.spawn()的stdio。除非指定stdio,否则默认情况下stderr将输出到父进程的标准错误。默认值:'pipe'。\
stdio<string> | <Array> Child's stdio configuration. Seechild_process.spawn()'sstdio.stderrby default will be output to the parent process' stderr unlessstdiois specified. 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 (seesetuid(2)). -
gid<number> 设置进程的组标识(请参阅setgid(2))。\
gid<number> Sets the group identity of the process (seesetgid(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. See caveat atmaxBufferand Unicode. Default:1024 * 1024. -
encoding<string> 用于所有标准输入输出的输入和输出的编码。默认值:'buffer'。\
encoding<string> The encoding used for all stdio inputs and outputs. Default:'buffer'. -
windowsHide<boolean> 隐藏通常在 Windows 系统上创建的子进程控制台窗口。默认值:false。\
windowsHide<boolean> Hide the subprocess console window that would normally be created on Windows systems. Default:false. -
shell<boolean> | <string> 如果是true,则在 shell 内运行command。在 Unix 上使用'/bin/sh',在 Windows 上使用process.env.ComSpec。可以将不同的 shell 指定为字符串。参见 Shell 要求 和 默认 Windows shell。默认值:false(无壳)。\
shell<boolean> | <string> Iftrue, runscommandinside of a shell. Uses'/bin/sh'on Unix, andprocess.env.ComSpecon Windows. A different shell can be specified as a string. See Shell requirements and Default Windows shell. Default:false(no shell).
-
child_process.execFileSync() 方法通常与 child_process.execFile() 相同,只是该方法在子进程完全关闭之前不会返回。当遇到超时并发送 killSignal 时,该方法将在进程完全退出之前不会返回。
\The child_process.execFileSync() method is generally identical to
child_process.execFile() 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.
如果子进程拦截并处理了 SIGTERM 信号并没有退出,父进程仍然会等待,直到子进程退出。
\If the child process intercepts and handles the SIGTERM signal and
does not exit, the parent process will still 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 an
Error that will include the full result of the underlying
child_process.spawnSync().
如果启用了 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.
const { execFileSync } = require('node:child_process'); try { const stdout = execFileSync('my-script.sh', ['my-arg'], { // Capture stdout and stderr from child process. Overrides the // default behavior of streaming child stderr to the parent stderr stdio: 'pipe', // Use utf8 encoding for stdio pipes encoding: 'utf8', }); console.log(stdout); } catch (err) { if (err.code) { // Spawning child process failed console.error(err.code); } else { // Child was spawned but exited with non-zero exit code // Error contains any stdout and stderr from the child const { stdout, stderr } = err; console.error({ stdout, stderr }); } }import { execFileSync } from 'node:child_process'; try { const stdout = execFileSync('my-script.sh', ['my-arg'], { // Capture stdout and stderr from child process. Overrides the // default behavior of streaming child stderr to the parent stderr stdio: 'pipe', // Use utf8 encoding for stdio pipes encoding: 'utf8', }); console.log(stdout); } catch (err) { if (err.code) { // Spawning child process failed console.error(err.code); } else { // Child was spawned but exited with non-zero exit code // Error contains any stdout and stderr from the child const { stdout, stderr } = err; console.error({ stdout, stderr }); } }