[][src]Module tokio::signal

This is supported on feature="signal" only.

Asynchronous signal handling for Tokio

The primary type exported from this crate, unix::Signal, allows listening for arbitrary signals on Unix platforms, receiving them in an asynchronous fashion.

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.

The are some fundamental limitations of this crate documented on the Signal structure as well.

Examples

Print on "ctrl-c" notification.

use tokio::signal;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    signal::ctrl_c().await?;
    println!("ctrl-c received!");
    Ok(())
}

Wait for SIGHUP on Unix


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

#[tokio::main]
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 {
        stream.recv().await;
        println!("got signal HUP");
    }
}

Modules

unixfeature="signal"

Unix-specific types for signal handling.

Functions

ctrl_cfeature="signal"

Completes when a "ctrl-c" notification is sent to the process.