Module basic module

Module child_process

Child process management module

Reference method:

1 2
var child_process = require("child_process"); var child = child_process.spawn("ls");

When creating a child process, the options.stdio option is used to configure the pipeline established between the parent process and the child process. By default, the stdin, stdout and stderr of the child process will be redirected toChildProcessThe corresponding stdin, stdout, and stderr streams on the object. This is equivalent to setting options.stdio to ['pipe','pipe','pipe'].

For convenience, options.stdio can be one of the following strings:

  • 'pipe': equivalent to ['pipe','pipe','pipe'] (default value).
  • 'ignore': equivalent to ['ignore','ignore','ignore'].
  • 'inherit': equivalent to ['inherit','inherit','inherit'] or [0, 1, 2].
  • 'pty': equivalent to ['pty','pty','pty']. Windows is not supported.

Otherwise, the value of options.stdio needs to be an array (where each index corresponds to a file descriptor in the child process). File descriptors 0, 1, and 2 correspond to stdin, stdout, and stderr, respectively. Other file descriptors can be designated to create other pipes between the parent process and the child process. The value can be one of the following:

  1. 'pipe': Create a pipe between the child process and the parent process. The parent of the pipe is exposed to the parent process as the stdio[fd] attribute on the child_process object. The pipes created for file descriptors 0, 1, and 2 can also be used as stdin, stdout, and stderr, respectively.
  2. 'ignore': Instruct fibjs to ignore the file descriptor in the child process. Although fibjs will always open file descriptors 0, 1, and 2 for its spawned processes, setting the file descriptor to'ignore' enables fibjs to open /dev/null and attach it to the file descriptor of the child process.
  3. 'inherit': Pass the corresponding stdio to or from the parent process. In the first three positions, this is equivalent toprocess.stdin, process.stdout with process.stderr. In any other position, it is equivalent to'ignore'.
  4. 'pty': The child process will be executed in a virtual terminal. At this time, only stdin and stdout are valid.
  5. Positive integer: The integer value will be interpreted as the file descriptor currently opened in the parent process. It is shared with the child process, similar to sharing<Stream> The way of the object. Incoming sockets are not supported on Windows.
  6. null or undefined: use the default value. For stdio file descriptors 0, 1, and 2 (in other words, stdin, stdout, and stderr), pipes will be created. For file descriptor 3 and greater, the default is'ignore'.
1 2 3 4 5 6 7 8 9 10 11 12 13
const { spawn } = require('child_process'); // 子进程使用父进程的 stdio。 spawn('prg', [], { stdio: 'inherit' }); // 衍生的子进程只共享 stderr。 spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

For users who use nodejs at the same time, please pay attention

  • fibjs the child_process.exec(command, args)same name api nodejs similar functions, but in Windows, does not automatically cmd.exe command as an execution environment parameter;
  • The child_process.[spawn|exec|execFile|run] of fibjs is a synchronous/callback integrated async style function:
    • If the last parameter is not a function, it is synchronous
    • If a function is passed as the last parameter, it is asynchronous;
  • The return result of fibjs child_process.[exec|execFile] is an object, which is completely different from the object returned by the nodejs api with the same name
  • fibjs is child_process.runno corresponding API in the nodejs

Static function

spawn

Release a child process with the given command

1 2 3
static ChildProcess child_process.spawn(String command, Array args, Object options = {});

Call parameters:

  • command: String, specify the command to run
  • args: Array, specify the list of string parameters
  • options: Object, specify creation parameters

Return result:

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "stdio": Array | String, // 子进程 stdio 配置 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

Release a child process with the given command

1 2
static ChildProcess child_process.spawn(String command, Object options = {});

Call parameters:

  • command: String, specify the command to run
  • options: Object, specify creation parameters

Return result:

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "stdio": Array | String, // 子进程 stdio 配置 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

exec

Execute a command in the shell and buffer the output. When executed in callback mode, the function will return the child process object

1 2
static (Variant stdout, Variant stderr) child_process.exec(String command, Object options = {}) async;

Call parameters:

  • command: String, specify the command to run
  • options: Object, specify creation parameters

Return result:

  • (Variant stdout, Variant stderr), returns the stdio output content of the child process

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "env": {}, // 环境变量的键值对 "encoding": "utf8", // 指定返回结果的编码,缺省为 utf8 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

execFile

Directly execute the specified file and buffer the output. When executed in callback mode, the function will return the child process object

1 2 3
static (Variant stdout, Variant stderr) child_process.execFile(String command, Array args, Object options = {}) async;

Call parameters:

  • command: String, specify the command to run
  • args: Array, specify the list of string parameters
  • options: Object, specify creation parameters

Return result:

  • (Variant stdout, Variant stderr), returns the stdio output content of the child process

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "env": {}, // 环境变量的键值对 "encoding": "utf8", // 指定返回结果的编码,缺省为 utf8 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

Directly execute the specified file and buffer the output. When executed in callback mode, the function will return the child process object

1 2
static (Variant stdout, Variant stderr) child_process.execFile(String command, Object options = {}) async;

Call parameters:

  • command: String, specify the command to run
  • options: Object, specify creation parameters

Return result:

  • (Variant stdout, Variant stderr), returns the stdio output content of the child process

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "env": {}, // 环境变量的键值对 "encoding": "utf8", // 指定返回结果的编码,缺省为 utf8 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

fork

Execute a module in the child process

1 2 3
static ChildProcess child_process.fork(String module, Array args, Object options = {});

Call parameters:

  • module: String, specify the command to run
  • args: Array, specify the list of string parameters
  • options: Object, specify creation parameters

Return result:

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "stdio": Array | String, // 子进程 stdio 配置 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

Execute a module in the child process

1 2
static ChildProcess child_process.fork(String module, Object options = {});

Call parameters:

  • module: String, specify the command to run
  • options: Object, specify creation parameters

Return result:

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "stdio": Array | String, // 子进程 stdio 配置 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

run

Execute the specified file directly and return exitCode. When executed in callback mode, the function will return the child process object

1 2 3
static Integer child_process.run(String command, Array args, Object options = {}) async;

Call parameters:

  • command: String, specify the command to run
  • args: Array, specify the list of string parameters
  • options: Object, specify creation parameters

Return result:

  • Integer, Return the exitCode of the child process

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }

Execute the specified file directly and return exitCode. When executed in callback mode, the function will return the child process object

1 2
static Integer child_process.run(String command, Object options = {}) async;

Call parameters:

  • command: String, specify the command to run
  • options: Object, specify creation parameters

Return result:

  • Integer, Return the exitCode of the child process

The content supported by options is as follows:

1 2 3 4 5 6 7 8 9
{ "cwd": "", // 子进程的当前的工作目录,缺省使用当前目录 "env": {}, // 环境变量的键值对 "detached": false, // 子进程将会变成一个进程组的领导者,缺省为 false "uid": 0, // 设置用户进程的ID "gid": 0, // 设置进程组的ID "windowsVerbatimArguments": false, // 在 Windows上不执行引号或转义参数。 在 Unix 上被忽略。 当指定外壳且为 CMD 时,此选项将自动设置为true,缺省为 false "windowsHide": false // 隐藏通常在Windows系统上创建的子进程控制台窗口,缺省为 false }