19 stable releases (3 major)

3.1.8 Feb 23, 2021
3.1.7 Oct 11, 2020
3.1.6 Jul 29, 2020
3.1.4 Feb 17, 2020
0.1.1 Jul 16, 2015

#2 in Operating systems

Download history 17253/week @ 2020-12-21 16349/week @ 2020-12-28 26532/week @ 2021-01-04 28699/week @ 2021-01-11 26077/week @ 2021-01-18 25920/week @ 2021-01-25 27343/week @ 2021-02-01 27867/week @ 2021-02-08 34122/week @ 2021-02-15 35225/week @ 2021-02-22 36356/week @ 2021-03-01 39759/week @ 2021-03-08 41933/week @ 2021-03-15 38428/week @ 2021-03-22 34682/week @ 2021-03-29 43605/week @ 2021-04-05

116,624 downloads per month
Used in 277 crates (233 directly)

MIT/Apache

25KB
429 lines

CtrlC

Build Status Build status

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

Documentation

Example usage

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

fn main() {
    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();
    ctrlc::set_handler(move || {
        r.store(false, Ordering::SeqCst);
    }).expect("Error setting Ctrl-C handler");
    println!("Waiting for Ctrl-C...");
    while running.load(Ordering::SeqCst) {}
    println!("Got it! Exiting...");
}

Try the example yourself

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

Handling SIGTERM

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

[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

~0–295KB