2 stable releases

3.2.2 Oct 17, 2021

#230 in Operating systems

Download history 243/week @ 2024-07-21 286/week @ 2024-07-28 291/week @ 2024-08-04 365/week @ 2024-08-11 161/week @ 2024-08-18 218/week @ 2024-08-25 160/week @ 2024-09-01 224/week @ 2024-09-08 146/week @ 2024-09-15 237/week @ 2024-09-22 262/week @ 2024-09-29 238/week @ 2024-10-06 240/week @ 2024-10-13 336/week @ 2024-10-20 201/week @ 2024-10-27 358/week @ 2024-11-03

1,139 downloads per month
Used in 8 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–14MB
~186K SLoC