termination.md
This file
Path
docs/termination.md
203 lines · 7.0 KiB · Markdown
Last changed by
Document `SIGQUIT` (#1022)ehmickybefore adoptionc5bd9d8
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–10why, one step back1<picture>2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 🏁 Termination89## Canceling10Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 11why, one step back11The [`cancelSignal`](api.md#optionscancelsignal) option can be used to cancel a subprocess. When [`abortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) is [aborted](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort), a [`SIGTERM` signal](#default-signal) is sent to the subprocess.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 12–34why, one step back1213```js14import {execa} from 'execa';1516const abortController = new AbortController();1718setTimeout(() => {19 abortController.abort();20}, 5000);2122try {23 await execa({cancelSignal: abortController.signal})`npm run build`;24} catch (error) {25 if (error.isCanceled) {26 console.error('Aborted by cancelSignal.');27 }2829 throw error;30}31```3233## Timeout34Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 35why, one step back35If the subprocess lasts longer than the [`timeout`](api.md#optionstimeout) option, a [`SIGTERM` signal](#default-signal) is sent to it.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 36–51why, one step back3637```js38try {39 await execa({timeout: 5000})`npm run build`;40} catch (error) {41 if (error.timedOut) {42 console.error('Timed out.');43 }4445 throw error;46}47```4849## Current process exit5051If the current process exits, the subprocess is automatically [terminated](#default-signal) unless either:Move API reference to its own page (#996)ehmickybefore adoption7cde496
lines 52–53why, one step back52- The [`cleanup`](api.md#optionscleanup) option is `false`.53- The subprocess is run in the background using the [`detached`](api.md#optionsdetached) option.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 54–57why, one step back54- The current process was terminated abruptly, for example, with [`SIGKILL`](#sigkill) as opposed to [`SIGTERM`](#sigterm) or a successful exit.5556## Signal termination57Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 58why, one step back58[`subprocess.kill()`](api.md#subprocesskillsignal-error) sends a [signal](https://en.wikipedia.org/wiki/Signal_(IPC)) to the subprocess. This is an inter-process message handled by the OS. Most (but [not all](https://github.com/ehmicky/human-signals#action)) signals terminate the subprocess.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 59–89why, one step back5960[More info.](https://nodejs.org/api/child_process.html#subprocesskillsignal)6162### SIGTERM6364[`SIGTERM`](https://en.wikipedia.org/wiki/Signal_(IPC)#SIGTERM) is the default signal. It terminates the subprocess.6566```js67const subprocess = execa`npm run build`;68subprocess.kill();69// Is the same as:70subprocess.kill('SIGTERM');71```7273The subprocess can [handle that signal](https://nodejs.org/api/process.html#process_signal_events) to run some cleanup logic.7475```js76process.on('SIGTERM', () => {77 cleanup();78 process.exit(1);79})80```8182### SIGKILL8384[`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.8586```js87subprocess.kill('SIGKILL');88```89Document `SIGQUIT` (#1022)ehmickybefore adoptionc5bd9d8
lines 90–97why, one step back90### SIGQUIT9192[`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).9394```js95subprocess.kill('SIGQUIT');96```97Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 98–103why, one step back98### Other signals99100Other signals can be passed as argument. However, most other signals do not fully [work on Windows](https://github.com/ehmicky/cross-platform-node-guide/blob/main/docs/6_networking_ipc/signals.md#cross-platform-signals).101102### Default signal103Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 104why, one step back104The [`killSignal`](api.md#optionskillsignal) option sets the default signal used by [`subprocess.kill()`](api.md#subprocesskillsignal-error) and the following options: [`cancelSignal`](#canceling), [`timeout`](#timeout), [`maxBuffer`](output.md#big-output) and [`cleanup`](#current-process-exit). It is [`SIGTERM`](#sigterm) by default.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 105–112why, one step back105106```js107const subprocess = execa({killSignal: 'SIGKILL'})`npm run build`;108subprocess.kill(); // Forceful termination109```110111### Signal name and description112Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec
line 113why, one step back113When a subprocess was terminated by a signal, [`error.isTerminated`](api.md#erroristerminated) is `true`.Add user guides (#989)ehmickybefore adoptionbe91ecf
line 114why, one step back114Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec
line 115why, one step back115Also, [`error.signal`](api.md#errorsignal) and [`error.signalDescription`](api.md#errorsignaldescription) indicate the signal's name and [human-friendly description](https://github.com/ehmicky/human-signals). On Windows, those are only set if the current process terminated the subprocess, as opposed to [another process](#inter-process-termination).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 116–133why, one step back116117```js118try {119 await execa`npm run build`;120} catch (error) {121 if (error.isTerminated) {122 console.error(error.signal); // SIGFPE123 console.error(error.signalDescription); // 'Floating point arithmetic error'124 }125126 throw error;127}128```129130## Forceful termination131132If the subprocess is terminated but does not exit, [`SIGKILL`](#sigkill) is automatically sent to forcefully terminate it.133Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 134why, one step back134The grace period is set by the [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option, which is 5 seconds by default. This feature can be disabled with `false`.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 135–136why, one step back135136This works when the subprocess is terminated by either:Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 137why, one step back137- Calling [`subprocess.kill()`](api.md#subprocesskillsignal-error) with no arguments.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 138–140why, one step back138- The [`cancelSignal`](#canceling), [`timeout`](#timeout), [`maxBuffer`](output.md#big-output) or [`cleanup`](#current-process-exit) option.139140This does not work when the subprocess is terminated by either:Move API reference to its own page (#996)ehmickybefore adoption7cde496
lines 141–142why, one step back141- Calling [`subprocess.kill()`](api.md#subprocesskillsignal-error) with a specific signal.142- Calling [`process.kill(subprocess.pid)`](api.md#subprocesspid).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 143–154why, one step back143- Sending a termination signal [from another process](#inter-process-termination).144145Also, this does not work on Windows, because Windows [doesn't support signals](https://nodejs.org/api/process.html#process_signal_events): `SIGKILL` and `SIGTERM` both terminate the subprocess immediately. Other packages (such as [`taskkill`](https://github.com/sindresorhus/taskkill)) can be used to achieve fail-safe termination on Windows.146147```js148// No forceful termination149const subprocess = execa({forceKillAfterDelay: false})`npm run build`;150subprocess.kill();151```152153## Inter-process termination154Document `process.kill(subprocess.pid)` (#1005)ehmickybefore adoptionf8d4e08
line 155why, one step back155[`subprocess.kill()`](api.md#subprocesskillsignal-error) only works when the current process terminates the subprocess. To terminate the subprocess from a different process, its [`subprocess.pid`](api.md#subprocesspid) can be used instead.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 156–162why, one step back156157```js158const subprocess = execa`npm run build`;159console.log('PID:', subprocess.pid); // PID: 6513160await subprocess;161```162Document `process.kill(subprocess.pid)` (#1005)ehmickybefore adoptionf8d4e08
lines 163–164why, one step back163For example, from a terminal:164Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 165–166why, one step back165```sh166$ kill -SIGTERM 6513Document `process.kill(subprocess.pid)` (#1005)ehmickybefore adoptionf8d4e08
lines 167–174why, one step back167```168169Or from a different Node.js process:170171```js172import process from 'node:process';173174process.kill(subprocessPid);Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 175–178why, one step back175```176177## Error message and stack trace178Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 179why, one step back179When terminating a subprocess, it is possible to include an error message and stack trace by using [`subprocess.kill(error)`](api.md#subprocesskillerror). The `error` argument will be available at [`error.cause`](api.md#errorcause).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 180–203why, one step back180181```js182try {183 const subprocess = execa`npm run build`;184 setTimeout(() => {185 subprocess.kill(new Error('Timed out after 5 seconds.'));186 }, 5000);187 await subprocess;188} catch (error) {189 if (error.isTerminated) {190 console.error(error.cause); // new Error('Timed out after 5 seconds.')191 console.error(error.cause.stack); // Stack trace from `error.cause`192 console.error(error.originalMessage); // 'Timed out after 5 seconds.'193 }194195 throw error;196}197```198199<hr>200201[**Next**: 🎹 Input](input.md)\202[**Previous**: ❌ Errors](errors.md)\203[**Top**: Table of contents](../readme.md#documentation)