2 stable releases

3.2.2 Oct 17, 2021

#309 in Operating systems

Download history 26/week @ 2023-12-17 25/week @ 2023-12-24 2/week @ 2023-12-31 68/week @ 2024-01-07 80/week @ 2024-01-14 21/week @ 2024-01-21 61/week @ 2024-01-28 19/week @ 2024-02-04 55/week @ 2024-02-11 114/week @ 2024-02-18 72/week @ 2024-02-25 58/week @ 2024-03-03 102/week @ 2024-03-10 105/week @ 2024-03-17 116/week @ 2024-03-24 116/week @ 2024-03-31

445 downloads per month
Used in 7 crates

MIT/Apache

33KB
583 lines

CtrlC

Build Status Build status

A simple easy to use wrapper around Ctrl-C signal.

Documentation

Example usage

use std::sync::mpsc::channel;
use ctrlc;

fn main() {
    let (tx, rx) = channel();
    
    ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
        .expect("Error setting Ctrl-C handler");
    
    println!("Waiting for Ctrl-C...");
    rx.recv().expect("Could not receive from channel.");
    println!("Got it! Exiting..."); 
}

Try the example yourself

cargo build --examples && target/debug/examples/readme_example

Handling SIGTERM and SIGHUP

Add CtrlC to Cargo.toml using termination feature and CtrlC will handle SIGINT, SIGTERM and SIGHUP.

[dependencies]
ctrlc = { version = "3.0", features = ["termination"] }

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4–16MB
~190K SLoC