Lines Matching full:process
1 # Child process
26 console.log(`child process exited with code ${code}`);
31 the parent Node.js process and the spawned subprocess. These pipes have
39 variable if it is in the `options` object. Otherwise, `process.env.PATH` is
49 The [`child_process.spawn()`][] method spawns the child process asynchronously,
52 the event loop until the spawned process either exits or is terminated.
65 * [`child_process.fork()`][]: spawns a new Node.js process and invokes a
78 ## Asynchronous process creation
85 implement the Node.js [`EventEmitter`][] API, allowing the parent process to
87 the life cycle of the child process.
91 invoked when the child process terminates.
163 * `cwd` {string|URL} Current working directory of the child process.
164 **Default:** `process.cwd()`.
165 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
169 `'/bin/sh'` on Unix, `process.env.ComSpec` on Windows.
170 * `signal` {AbortSignal} allows aborting the child process using an
174 stderr. If exceeded, the child process is terminated and any output is
178 * `uid` {number} Sets the user identity of the process (see setuid(2)).
179 * `gid` {number} Sets the group identity of the process (see setgid(2)).
182 * `callback` {Function} called with the output when process terminates.
211 the exit code of the process. By convention, any exit code other than `0`
213 process.
216 stdout and stderr output of the child process. By default, Node.js will decode
239 the existing process and uses a shell to execute the command.
261 `AbortController` is similar to calling `.kill()` on the child process except
293 * `cwd` {string|URL} Current working directory of the child process.
294 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
298 stderr. If exceeded, the child process is terminated and any output is
302 * `uid` {number} Sets the user identity of the process (see setuid(2)).
303 * `gid` {number} Sets the group identity of the process (see setgid(2)).
309 `'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different
312 * `signal` {AbortSignal} allows aborting the child process using an
314 * `callback` {Function} Called with the output when process terminates.
322 executable `file` is spawned directly as a new process making it slightly more
340 stdout and stderr output of the child process. By default, Node.js will decode
368 `AbortController` is similar to calling `.kill()` on the child process except
414 * `cwd` {string|URL} Current working directory of the child process.
416 process. Specific behavior depends on the platform, see
418 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
419 * `execPath` {string} Executable used to create the child process.
421 **Default:** `process.execArgv`.
422 * `gid` {number} Sets the group identity of the process (see setgid(2)).
426 * `signal` {AbortSignal} Allows closing the child process using an
429 process will be killed by timeout or abort signal. **Default:** `'SIGTERM'`.
438 * `uid` {number} Sets the user identity of the process (see setuid(2)).
441 * `timeout` {number} In milliseconds the maximum amount of time the process
454 that is established between the two. Each process has its own memory, with
460 [`process.execPath`][] of the parent process. The `execPath` property in the
464 parent process using the file descriptor (fd) identified using the
465 environment variable `NODE_CHANNEL_FD` on the child process.
468 current process.
474 `AbortController` is similar to calling `.kill()` on the child process except
478 if (process.argv[2] === 'child') {
480 console.log(`Hello from ${process.argv[2]}!`);
490 controller.abort(); // Stops the child process
530 * `cwd` {string|URL} Current working directory of the child process.
531 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
533 process. This will be set to `command` if not specified.
537 process. Specific behavior depends on the platform, see
539 * `uid` {number} Sets the user identity of the process (see setuid(2)).
540 * `gid` {number} Sets the group identity of the process (see setgid(2)).
545 `'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different
553 * `signal` {AbortSignal} allows aborting the child process using an
555 * `timeout` {number} In milliseconds the maximum amount of time the process
558 process will be killed by timeout or abort signal. **Default:** `'SIGTERM'`.
562 The `child_process.spawn()` method spawns a new process using the given
575 env: process.env
579 Use `cwd` to specify the working directory from which the process is spawned.
581 but the path does not exist, the child process emits an `ENOENT` error
586 process, the default is [`process.env`][].
606 console.log(`child process exited with code ${code}`);
627 console.log(`ps process exited with code ${code}`);
642 console.log(`grep process exited with code ${code}`);
658 Certain platforms (macOS, Linux) will use the value of `argv[0]` for the process
661 Node.js currently overwrites `argv[0]` with `process.execPath` on startup, so
662 `process.argv[0]` in a Node.js child process will not match the `argv0`
664 `process.argv0` property instead.
667 `AbortController` is similar to calling `.kill()` on the child process except
678 controller.abort(); // Stops the child process
687 child process to continue running after the parent exits. The child will have
688 its own console window. Once enabled for a child process, it cannot be
692 process will be made the leader of a new process group and session. Child
703 When using the `detached` option to start a long-running process, the process
709 Example of a long-running process, by detaching and also ignoring its parent
715 const subprocess = spawn(process.argv[0], ['child_program.js'], {
723 Alternatively one can redirect the child process' output into files:
752 between the parent and child process. By default, the child's stdin, stdout,
770 1. `'pipe'`: Create a pipe between the child process and the parent process.
777 process's stdio handles. See the
784 [`subprocess.send()`][] method. If the child is a Node.js process, the
785 presence of an IPC channel will enable [`process.send()`][] and
786 [`process.disconnect()`][] methods, as well as [`'disconnect'`][] and
789 Accessing the IPC channel fd in any way other than [`process.send()`][]
790 or using the IPC channel with a child process that is not a Node.js instance
797 parent process. In the first three positions, this is equivalent to
798 `process.stdin`, `process.stdout`, and `process.stderr`, respectively. In
801 file, socket, or a pipe with the child process. The stream's underlying
802 file descriptor is duplicated in the child process to the fd that
807 that is currently open in the parent process. It is shared with the child
808 process, similar to how {Stream} objects can be shared. Passing sockets
821 spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
829 parent and child processes, and the child is a Node.js process, the child
833 normally without the process being held open by the open IPC channel.*
843 ## Synchronous process creation
848 process exits.
882 * `cwd` {string|URL} Current working directory of the child process.
884 as stdin to the spawned process. Supplying this value will override
887 be output to the parent process' stderr unless `stdio` is specified.
889 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
890 * `uid` {number} Sets the user identity of the process (see setuid(2)).
891 * `gid` {number} Sets the group identity of the process (see setgid(2)).
892 * `timeout` {number} In milliseconds the maximum amount of time the process
895 process will be killed. **Default:** `'SIGTERM'`.
897 stderr. If exceeded, the child process is terminated. See caveat at
904 `'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different
911 return until the child process has fully closed. When a timeout has been
912 encountered and `killSignal` is sent, the method won't return until the process
915 If the child process intercepts and handles the `SIGTERM` signal and
916 does not exit, the parent process will still wait until the child process has
919 If the process times out or has a non-zero exit code, this method will throw an
949 * `cwd` {string|URL} Current working directory of the child process.
951 as stdin to the spawned process. Supplying this value will override
954 be output to the parent process' stderr unless `stdio` is specified.
956 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
959 `'/bin/sh'` on Unix, `process.env.ComSpec` on Windows.
960 * `uid` {number} Sets the user identity of the process. (See setuid(2)).
961 * `gid` {number} Sets the group identity of the process. (See setgid(2)).
962 * `timeout` {number} In milliseconds the maximum amount of time the process
965 process will be killed. **Default:** `'SIGTERM'`.
967 stderr. If exceeded, the child process is terminated and any output is
978 until the child process has fully closed. When a timeout has been encountered
979 and `killSignal` is sent, the method won't return until the process has
980 completely exited. If the child process intercepts and handles the `SIGTERM`
981 signal and doesn't exit, the parent process will wait until the child process
984 If the process times out or has a non-zero exit code, this method will throw.
1022 * `cwd` {string|URL} Current working directory of the child process.
1024 as stdin to the spawned process. Supplying this value will override
1027 process. This will be set to `command` if not specified.
1029 * `env` {Object} Environment key-value pairs. **Default:** `process.env`.
1030 * `uid` {number} Sets the user identity of the process (see setuid(2)).
1031 * `gid` {number} Sets the group identity of the process (see setgid(2)).
1032 * `timeout` {number} In milliseconds the maximum amount of time the process
1035 process will be killed. **Default:** `'SIGTERM'`.
1037 stderr. If exceeded, the child process is terminated and any output is
1043 `'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different
1052 * `pid` {number} Pid of the child process.
1060 * `error` {Error} The error object if the child process failed or timed out.
1064 until the child process has fully closed. When a timeout has been encountered
1065 and `killSignal` is sent, the method won't return until the process has
1066 completely exited. If the process intercepts and handles the `SIGTERM` signal
1067 and doesn't exit, the parent process will wait until the child process has
1094 * `signal` {string} The signal by which the child process was terminated.
1096 The `'close'` event is emitted after a process has ended _and_ the stdio
1097 streams of a child process have been closed. This is distinct from the
1111 console.log(`child process close all stdio with code ${code}`);
1115 console.log(`child process exited with code ${code}`);
1125 [`subprocess.disconnect()`][] method in parent process or
1126 [`process.disconnect()`][] in child process. After disconnecting it is no longer
1136 1. The process could not be spawned, or
1137 2. The process could not be killed, or
1138 3. Sending a message to the child process failed.
1152 * `signal` {string} The signal by which the child process was terminated.
1154 The `'exit'` event is emitted after the child process ends. If the process
1155 exited, `code` is the final exit code of the process, otherwise `null`. If the
1156 process terminated due to receipt of a signal, `signal` is the string name of
1159 When the `'exit'` event is triggered, child process stdio streams might still be
1178 The `'message'` event is triggered when a child process uses
1179 [`process.send()`][] to send messages.
1185 child process, the `message` argument can contain data that JSON is not able
1194 The `'spawn'` event is emitted once the child process has spawned successfully.
1195 If the child process does not spawn successfully, the `'spawn'` event is not
1202 the spawned process. For example, if `bash some-command` spawns successfully,
1215 * {Object} A pipe representing the IPC channel to the child process.
1225 This method makes the IPC channel keep the event loop of the parent process
1233 This method makes the IPC channel not keep the event loop of the parent process
1244 send and receive messages from a child process. When `subprocess.connected` is
1254 this method the `subprocess.connected` and `process.connected` properties in
1259 process of being received. This will most often be triggered immediately after
1262 When the child process is a Node.js instance (e.g. spawned using
1263 [`child_process.fork()`][]), the `process.disconnect()` method can be invoked
1264 within the child process to close the IPC channel as well.
1270 The `subprocess.exitCode` property indicates the exit code of the child process.
1271 If the child process is still running, the field will be `null`.
1281 The `subprocess.kill()` method sends a signal to the child process. If no
1282 argument is given, the process will be sent the `'SIGTERM'` signal. See
1292 `child process terminated due to receipt of signal ${signal}`);
1295 // Send SIGHUP to process.
1300 cannot be delivered. Sending a signal to a child process that has already exited
1302 process identifier (PID) has been reassigned to another process, the signal will
1303 be delivered to that process instead which can have unexpected results.
1305 While the function is called `kill`, the signal delivered to the child process
1306 may not actually terminate the process.
1311 ignored, and the process will be killed forcefully and abruptly (similar to
1317 new process in a shell or with the use of the `shell` option of `ChildProcess`:
1328 console.log(process.pid, 'is alive')
1336 subprocess.kill(); // Does not terminate the Node.js process in the shell.
1346 send a signal to the child process.
1348 The `subprocess.killed` property indicates whether the child process
1350 does not indicate that the child process has been terminated.
1359 Returns the process identifier (PID) of the child process. If the child process
1377 restore the removed reference count for the child process, forcing the parent
1383 const subprocess = spawn(process.argv[0], ['child_program.js'], {
1414 `net.Socket`. When `true`, the socket is kept open in the sending process.
1421 be used to send messages to the child process. When the child process is a
1444 process.on('message', (m) => {
1449 process.send({ foo: 'bar', baz: NaN });
1452 Child Node.js processes will have a [`process.send()`][] method of their own
1464 for passing a TCP server or socket object to the child process. The child will
1475 happen, for instance, when the child process has already exited.
1485 a TCP server object to the child process as illustrated in the example below:
1503 process.on('message', (m, server) => {
1524 socket to the child process. The example below spawns two children that each
1533 // the sockets from being read before they are sent to the child process.
1552 process.on('message', (m, socket) => {
1557 // sent and the time it is received in the child process.
1558 socket.end(`Request handled with ${process.argv[2]} priority`);
1576 the child process if any, else `null`.
1583 arguments the child process was launched with.
1590 the child process that is launched.
1593 [`process.execPath`][].
1597 in which the child process is launched.
1606 A `Readable Stream` that represents the child process's `stderr`.
1614 The `subprocess.stderr` property can be `null` if the child process could
1624 A `Writable Stream` that represents the child process's `stdin`.
1626 If a child process waits to read all of its input, the child will not continue
1635 The `subprocess.stdin` property can be `undefined` if the child process could
1645 A sparse array of pipes to the child process, corresponding with positions in
1678 The `subprocess.stdio` property can be `undefined` if the child process could
1688 A `Readable Stream` that represents the child process's `stdout`.
1706 The `subprocess.stdout` property can be `null` if the child process could
1724 const subprocess = spawn(process.argv[0], ['child_program.js'], {
1735 or `stderr`. If this value is exceeded, then the child process is terminated.
1751 spawned, `'cmd.exe'` is used as a fallback if `process.env.ComSpec` is
1779 [Signal Events]: process.md#process_signal_events
1780 [`'disconnect'`]: process.md#process_event_disconnect
1783 [`'message'`]: process.md#process_event_message
1798 [`process.disconnect()`]: process.md#process_process_disconnect
1799 [`process.env`]: process.md#process_process_env
1800 [`process.execPath`]: process.md#process_process_execpath
1801 [`process.send()`]: process.md#process_process_send_message_sendhandle_options_callback