aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/process/os-signals.md
blob: ae7b9896fc109aabe7ecc57a42f74c12e323ecd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
name: Listen to OS signals
---

Bun supports the Node.js `process` global, including the `process.on()` method for listening to OS signals.

```ts
process.on("SIGINT", () => {
  console.log("Received SIGINT");
});
```

---

If you don't know which signal to listen for, you listen to the umbrella `"exit"` event.

```ts
process.on("exit", (code) => {
  console.log(`Process exited with code ${code}`);
});
```

---

If you don't know which signal to listen for, you listen to the [`"beforeExit"`](https://nodejs.org/api/process.html#event-beforeexit) and [`"exit"`](https://nodejs.org/api/process.html#event-exit) events.

```ts
process.on("beforeExit", (code) => {
  console.log(`Event loop is empty!`);
});

process.on("exit", (code) => {
  console.log(`Process is exiting with code ${code}`);
});
```

---

See [Docs > API > Utils](/docs/api/utils) for more useful utilities.