AphrodyBun GitHub

Runtime docs · Guides · Processes & System

Listen to OS signals

Handle OS signals such as SIGINT and the beforeExit and exit events with process.on().

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

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

To run code when the process exits, listen for these events:

  • "beforeExit": Bun emits this event when the event loop empties.
  • "exit": Bun emits this event when the event loop empties or when process.exit() is called.

Neither event is emitted when the process is killed by a signal it has no listener for. To run cleanup on a signal, listen for that signal and call process.exit() from the listener.

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

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

See Utils for more utilities.