📜  Process.ChildProcess._handle.onexit - Javascript (1)

📅  最后修改于: 2023-12-03 15:03:52.275000             🧑  作者: Mango

Process.ChildProcess._handle.onexit - JavaScript

The Process.ChildProcess._handle.onexit event in JavaScript is used to handle the exit event of a child process.

Introduction

In Node.js, a child process is a spawned subprocess that can be executed independently of the main process. These child processes are useful for running CPU-intensive tasks or performing blocking I/O operations without blocking the event loop of the main process.

Process.ChildProcess._handle.onexit Event

The Process.ChildProcess._handle.onexit event is emitted when a child process exits. It allows you to handle the exit event and perform any necessary operations. The event is fired once for each child process that has exited.

Syntax
process.childProcess._handle.onexit(<pid>, <exitCode>, <signal>);
  • pid: The process id of the child process.
  • exitCode: The exit code of the child process. Returns null if the child process was terminated by a signal.
  • signal: The signal that caused the child process to terminate. Returns null if the child process exited normally (i.e., not by a signal).
Example
const { spawn } = require('child_process');

const child = spawn('ls', ['-lh', '/usr']);

child.on('exit', (code, signal) => {
  console.log(`Child process exited with code ${code} and signal ${signal}`);
});

child.on('error', (err) => {
  console.error(`Error occurred: ${err.message}`);
});
Markdown Code Block
The `Process.ChildProcess._handle.onexit` event in JavaScript is used to handle the exit event of a child process. 

## Introduction
In Node.js, a child process is a spawned subprocess that can be executed independently of the main process. These child processes are useful for running CPU-intensive tasks or performing blocking I/O operations without blocking the event loop of the main process.

## `Process.ChildProcess._handle.onexit` Event
The `Process.ChildProcess._handle.onexit` event is emitted when a child process exits. It allows you to handle the exit event and perform any necessary operations. The event is fired once for each child process that has exited.

### Syntax
```javascript
process.childProcess._handle.onexit(<pid>, <exitCode>, <signal>);
  • pid: The process id of the child process.
  • exitCode: The exit code of the child process. Returns null if the child process was terminated by a signal.
  • signal: The signal that caused the child process to terminate. Returns null if the child process exited normally (i.e., not by a signal).
Example
const { spawn } = require('child_process');

const child = spawn('ls', ['-lh', '/usr']);

child.on('exit', (code, signal) => {
  console.log(`Child process exited with code ${code} and signal ${signal}`);
});

child.on('error', (err) => {
  console.error(`Error occurred: ${err.message}`);
});