How to catch signals in Rust

It seems that it’s now fairly trivial to implement this. The Signal handling section of Command Line Applications in Rust goes over the concept, and mentions the ctrlc crate to handle that specific signal, and the signal-hook crate to handle signals in general.

Via the guide, with signal-hook it should be as simple as:

use std::{error::Error, thread};
use signal_hook::{iterator::Signals, SIGTERM};

fn main() -> Result<(), Box<Error>> {
    let signals = Signals::new(&[SIGTERM])?;

    thread::spawn(move || {
        for sig in signals.forever() {
            println!("Received signal {:?}", sig);
        }
    });

    Ok(())
}

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.