Module tokio::signal

source ·
Expand description

Asynchronous signal handling for Tokio.

Note that signal handling is in general a very tricky topic and should be used with great care. This crate attempts to implement ‘best practice’ for signal handling, but it should be evaluated for your own applications’ needs to see if it’s suitable.

There are some fundamental limitations of this crate documented on the OS specific structures, as well.


Print on “ctrl-c” notification.

use tokio::signal;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("ctrl-c received!");

Wait for SIGHUP on Unix

use tokio::signal::unix::{signal, SignalKind};

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // An infinite stream of hangup signals.
    let mut stream = signal(SignalKind::hangup())?;

    // Print whenever a HUP signal is received
    loop {
        println!("got signal HUP");


  • Unix-specific types for signal handling.


  • Completes when a “ctrl-c” notification is sent to the process.