logo
GitHubEdit on GitHub

Handle OS Signals

This program makes use of an unstable Deno feature. Learn more about unstable features.

⚠️ Handling OS signals is currently not available on Windows.

Concepts

Set up an OS signal listener

APIs for handling OS signals are modelled after already familiar addEventListener and removeEventListener APIs.

⚠️ Note that listening for OS signals doesn't prevent event loop from finishing, ie. if there are no more pending async operations the process will exit.

You can use Deno.addSignalListener() function for handling OS signals:

/**
 * add_signal_listener.ts
 */
console.log("Press Ctrl-C to trigger a SIGINT signal");

Deno.addSignalListener("SIGINT", (_) => {
  console.log("interrupted!");
  Deno.exit();
});

// Add a timeout to prevent process exiting immediately.
setTimeout(() => {}, 5000);

Run with:

deno run --unstable add_signal_listener.ts

You can use Deno.removeSignalListener() function to unregister previously added signal handler.

/**
 * signal_listeners.ts
 */
console.log("Press Ctrl-C to trigger a SIGINT signal");

const sigIntHandler = (_) => {
  console.log("interrupted!");
  Deno.exit();
};
Deno.addSignalListener("SIGINT", sigIntHandler);

// Add a timeout to prevent process existing immediately.
setTimeout(() => {}, 5000);

// Stop listening for a signal after 1s.
setTimeout(() => {
  Deno.removeSignalListener("SIGINT", sigIntHandler);
}, 1000);

Run with:

deno run --unstable signal_listeners.ts

Async iterator example

If you prefer to handle signals using an async iterator, you can use signal() API available in deno_std:

/**
 * async_iterator_signal.ts
 */
import { signal } from "https://deno.land/std@0.126.0/signal/mod.ts";

const sig = signal("SIGUSR1", "SIGINT");

// Add a timeout to prevent process exiting immediately.
setTimeout(() => {}, 5000);

for await (const _ of sig) {
  console.log("interrupt or usr1 signal received");
}

Run with:

deno run --unstable async_iterator_signal.ts