모듈 기본 모듈

모듈 child_process

하위 프로세스 관리 모듈

인용하다:

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

하위 프로세스를 생성할 때 options.stdio 옵션을 사용하여 상위 프로세스와 하위 프로세스 사이에 설정된 파이프를 구성합니다. 기본적으로 하위 프로세스의 stdin, stdout 및 stderr은 다음으로 리디렉션됩니다.ChildProcess객체의 해당 stdin, stdout 및 stderr 스트림. 이는 options.stdio를 ['pipe', 'pipe', 'pipe']로 설정하는 것과 같습니다.

편의상 options.stdio는 다음 문자열 중 하나일 수 있습니다.

  • 'pipe': ['pipe', 'pipe', 'pipe'] (기본값)과 동일합니다.
  • '무시': ['무시', '무시', '무시']와 동일합니다.
  • '상속': ['상속', '상속', '상속'] 또는 [0, 1, 2]와 동일합니다.
  • 'pty': ['pty', 'pty', 'pty']와 동일합니다. 윈도우는 지원되지 않습니다.

그렇지 않은 경우 options.stdio의 값은 배열이어야 합니다(각 인덱스는 하위 프로세스의 파일 설명자에 해당함). 파일 설명자 0, 1, 2는 각각 stdin, stdout 및 stderr에 해당합니다. 상위 프로세스와 하위 프로세스 사이에 다른 파이프를 생성하기 위해 다른 파일 설명자를 지정할 수 있습니다. 값은 다음 중 하나일 수 있습니다.

  1. '파이프': 하위 프로세스와 상위 프로세스 사이에 파이프를 만듭니다. 파이프의 상위 끝은 child_process 객체의 stdio[fd] 속성으로 상위 프로세스에 노출됩니다. 파일 설명자 0, 1, 2에 대해 생성된 파이프는 각각 stdin, stdout 및 stderr로도 사용할 수 있습니다.
  2. 'ignore': fibjs에 하위 프로세스의 파일 설명자를 무시하도록 지시합니다. fibjs는 생성된 프로세스에 대해 항상 파일 설명자 0, 1, 2를 열지만, 파일 설명자를 '무시'로 설정하면 fibjs가 /dev/null을 열고 이를 하위 프로세스의 파일 설명자에 추가합니다.
  3. 'inherit': 해당 stdio를 상위 프로세스로 스트리밍하거나 상위 프로세스에서 스트리밍합니다. 처음 세 위치에서 이는 다음과 같습니다.process.stdin,process.stdout그리고process.stderr. 다른 위치에서는 '무시'와 동일합니다.
  4. 'pty': 하위 프로세스가 가상 터미널에서 실행됩니다. 현재로서는 stdin과 stdout만 유효합니다.
  5. 양의 정수: 정수 값은 현재 상위 프로세스에 열려 있는 파일 설명자로 해석됩니다. 공유 <와 유사하게 하위 프로세스와 공유됩니다.Stream> 객체 방식. Windows에서는 들어오는 소켓이 지원되지 않습니다.
  6. null 또는 정의되지 않음: 기본값을 사용합니다. 파이프는 stdio의 파일 설명자 0, 1, 2(즉, stdin, stdout 및 stderr)에 대해 생성됩니다. 파일 설명자 3 이상의 경우 기본값은 '무시'입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13
const { spawn } = require('child_process'); // child process uses parent's stdio spawn('prg', [], { stdio: 'inherit' }); // child process uses parent's stderr spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

nodejs도 사용하는 사용자의 경우 다음 사항에 유의하세요.

  • child_process.exec(command, args)같은 이름의 fibjs API의 기능은 nodejs와 유사하지만 Windows에서는 cmd.exe가 명령 매개변수의 실행 환경으로 자동으로 사용되지 않습니다.
  • fibjs의 child_process.[spawn|exec|execFile|run]은 동기화/콜백을 통합하는 비동기 스타일 함수입니다.
    • 마지막 매개변수가 함수가 아니면 동기화됩니다.
    • 함수가 마지막 매개변수로 전달되면 비동기식입니다.
  • fibjs의 child_process.[exec|execFile] 반환 결과는 동일한 이름의 nodejs API에서 반환한 개체와 완전히 다른 개체입니다 .
  • fibjs child_process.run에는 nodejs에 해당 API가 없습니다.

정적 함수

spawn

주어진 명령으로 하위 프로세스 게시

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • args: 배열, 문자열 매개변수 목록을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • ChildProcess, 하위 프로세스 객체를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // configure the group identity of the process "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

주어진 명령으로 하위 프로세스 게시

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • ChildProcess, 하위 프로세스 객체를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

exec

셸에서 명령을 실행하고 출력을 버퍼링합니다. 콜백 모드에서 실행되면 함수는 하위 프로세스 개체를 반환합니다.

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • (Variant stdout, Variant stderr), 하위 프로세스의 stdio 출력 콘텐츠를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "encoding": "utf8", // specify the character encoding used to decode the stdout and stderr output "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

execFile

지정된 파일을 직접 실행하고 출력을 버퍼링합니다. 콜백 모드에서 실행되면 함수는 하위 프로세스 객체를 반환합니다.

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • args: 배열, 문자열 매개변수 목록을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • (Variant stdout, Variant stderr), 하위 프로세스의 stdio 출력 콘텐츠를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "encoding": "utf8", // specify the character encoding used to decode the stdout and stderr output "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

지정된 파일을 직접 실행하고 출력을 버퍼링합니다. 콜백 모드에서 실행되면 함수는 하위 프로세스 객체를 반환합니다.

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • (Variant stdout, Variant stderr), 하위 프로세스의 stdio 출력 콘텐츠를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "encoding": "utf8", // specify the character encoding used to decode the stdout and stderr output "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

spawnSync

주어진 명령으로 하위 프로세스 게시

1 2 3
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status, Variant error) child_process.spawnSync(String command, Array args, Object options = {}) async;

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • args: 배열, 문자열 매개변수 목록을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • (정수 pid, NArray 출력, Variant stdout, Variant stderr, Integer status, Variant error), 하위 프로세스 실행 결과를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // configure the group identity of the process "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

주어진 명령으로 하위 프로세스 게시

1 2
static (Integer pid, NArray output, Variant stdout, Variant stderr, Integer status, Variant error) child_process.spawnSync(String command, Object options = {}) async;

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • (정수 pid, NArray 출력, Variant stdout, Variant stderr, Integer status, Variant error), 하위 프로세스 실행 결과를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

fork

하위 프로세스에서 모듈 실행

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

호출 매개변수:

  • module: 문자열, 실행할 명령을 지정합니다.
  • args: 배열, 문자열 매개변수 목록을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • ChildProcess, 하위 프로세스 객체를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

하위 프로세스에서 모듈 실행

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

호출 매개변수:

  • module: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • ChildProcess, 하위 프로세스 객체를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9 10
{ "cwd": "", // working directory of the child process, default to current directory "stdio": Array | String, // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

run

지정된 파일을 직접 실행하고 exitCode를 반환하며, 콜백 모드로 실행하면 자식 프로세스 객체를 반환합니다.

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • args: 배열, 문자열 매개변수 목록을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • Integer, 하위 프로세스의 종료 코드를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9
{ "cwd": "", // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }

지정된 파일을 직접 실행하고 exitCode를 반환하며, 콜백 모드로 실행하면 자식 프로세스 객체를 반환합니다.

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

호출 매개변수:

  • command: 문자열, 실행할 명령을 지정합니다.
  • options: 객체, 생성 매개변수 지정

결과 반환:

  • Integer, 하위 프로세스의 종료 코드를 반환합니다.

지원되는 옵션은 다음과 같습니다.

1 2 3 4 5 6 7 8 9
{ "cwd": "", // working directory of the child process, default to current directory "env": {}, // key-value pairs of environment variables to add to the child's environment "detached": false, // child process will be a leader of a new process group, default to false "uid": 0, // configure the user identity of the process "gid": 0, // con "windowsVerbatimArguments": false, // do not execute any quote or escape processing on Windows. Ignored on Unix. When specified, the command line string is passed directly to the underlying operating system shell without any processing whatsoever. This is set to true automatically when the shell option is specified and is CMD. "windowsHide": false // hide the subprocess console window that would normally be created on Windows systems. This option has no effect on non-Windows systems. }