34 stable releases (3 major)

3.4.5 Aug 13, 2024
3.4.4 Mar 10, 2024
3.4.2 Dec 22, 2023
3.4.1 Sep 3, 2023
0.1.1 Jul 16, 2015

#6 in Operating systems

Download history 320189/week @ 2024-07-27 302428/week @ 2024-08-03 347541/week @ 2024-08-10 377972/week @ 2024-08-17 388670/week @ 2024-08-24 357183/week @ 2024-08-31 424944/week @ 2024-09-07 381686/week @ 2024-09-14 438423/week @ 2024-09-21 439488/week @ 2024-09-28 468973/week @ 2024-10-05 451072/week @ 2024-10-12 478732/week @ 2024-10-19 473325/week @ 2024-10-26 461990/week @ 2024-11-02 398518/week @ 2024-11-09

1,891,758 downloads per month
Used in 1,127 crates (888 directly)

MIT/Apache

21KB
294 lines

CtrlC

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.5–9.5MB
~93K SLoC