33 stable releases (3 major)

3.4.4 Mar 10, 2024
3.4.2 Dec 22, 2023
3.4.1 Sep 3, 2023
3.4.0 May 30, 2023
0.1.1 Jul 16, 2015

#7 in Operating systems

Download history 229369/week @ 2024-01-05 230073/week @ 2024-01-12 254487/week @ 2024-01-19 239983/week @ 2024-01-26 256535/week @ 2024-02-02 261322/week @ 2024-02-09 267058/week @ 2024-02-16 303859/week @ 2024-02-23 281803/week @ 2024-03-01 281549/week @ 2024-03-08 267799/week @ 2024-03-15 272799/week @ 2024-03-22 288753/week @ 2024-03-29 294895/week @ 2024-04-05 311229/week @ 2024-04-12 266880/week @ 2024-04-19

1,210,141 downloads per month
Used in 943 crates (750 directly)

MIT/Apache

22KB
299 lines

CtrlC

Build Status Build status

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

Documentation

Example usage

In cargo.toml:

[dependencies]
ctrlc = "3.4"

then, in main.rs

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.

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.

Similar crates

There are alternatives that give you more control over the different signals and/or add async support.

Dependencies

~1–11MB
~103K SLoC