Skip to content
Myr

Improve documentation of signal handling (#1105)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 465e427 · Browse files · 2 files+21 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−9 removed

  • other:+21 added−9 removed

2 files+21 added−9 removed

modified:docs/api.md+1 added-1 removed
437 unmodified lines
438[More info.](streams.md#converting-a-subprocess-to-a-stream)439440## Result441442_TypeScript:_ [`Result`](typescript.md) or [`SyncResult`](typescript.md\442_TypeScript:_ [`Result`](typescript.md) or [`SyncResult`](typescript.md)\443_Type:_ `object`444445[Result](execution.md#result) of a subprocess successful execution.446
689 unmodified lines
modified:docs/termination.md+20 added-8 removed
66 unmodified lines
67[More info.](https://nodejs.org/api/child_process.html#subprocesskillsignal)6869### SIGTERM7071[`SIGTERM`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGTERM) is the default signal. It terminates the subprocess.71[`SIGTERM`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGTERM) is the default signal. It terminates the subprocess. On Unix, it can [be handled](#handling-signals) to run some cleanup logic.7273```js74const subprocess = execa`npm run build`;75subprocess.kill();76// Is the same as:77subprocess.kill('SIGTERM');78```7980The subprocess can [handle that signal](https://nodejs.org/api/process.html#process_signal_events) to run some cleanup logic.80### SIGINT8182[`SIGINT`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGINT) terminates the process. Its [handler](#handling-signals) is triggered on `CTRL-C`.8384```js83process.on('SIGTERM', () => {84 cleanup();85 process.exit(1);86})85subprocess.kill('SIGINT');86```8788### SIGKILL8991[`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL) is like [`SIGTERM`](#sigterm) except it forcefully terminates the subprocess, i.e. it does not allow it to handle the signal.90[`SIGKILL`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGKILL) forcefully terminates the subprocess. It [cannot be handled](#handling-signals).9192```js93subprocess.kill('SIGKILL');94```9596### SIGQUIT9799[`SIGQUIT`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGQUIT) is like [`SIGTERM`](#sigterm) except it creates a [core dump](https://en.wikipedia.org/wiki/Core_dump).98[`SIGQUIT`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGQUIT) terminates the process. On Unix, it creates a [core dump](https://en.wikipedia.org/wiki/Core_dump).99100```js101subprocess.kill('SIGQUIT');102```
10 unmodified lines
113const subprocess = execa({killSignal: 'SIGKILL'})`npm run build`;114subprocess.kill(); // Forceful termination115```116117### Handling signals118119On Unix, most signals (not [`SIGKILL`](#sigkill)) can be intercepted to perform a graceful exit.120121```js122process.on('SIGTERM', () => {123 cleanup();124 process.exit(1);125})126```127128Unfortunately this [usually does not work](https://github.com/ehmicky/cross-platform-node-guide/blob/main/docs/6_networking_ipc/signals.md#cross-platform-signals) on Windows. The only signal that is somewhat cross-platform is [`SIGINT`](#sigint): on Windows, its handler is triggered when the user types `CTRL-C` in the terminal. However `subprocess.kill('SIGINT')` is only handled on Unix.129130### Signal name and description131132When a subprocess was terminated by a signal, [`error.isTerminated`](api.md#erroristerminated) is `true`.133
89 unmodified lines