Easy Ctrl-C handler for Rust projects
Clone or download
Latest commit 0ba5f4b Nov 15, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Format code with latest rustfmt Apr 19, 2018
src Run rustfmt Nov 15, 2018
.gitignore Initial commit Feb 15, 2015
.travis.yml Fix syntax check on Travis Jun 4, 2018
Cargo.toml Release 3.1.1 Jun 4, 2018
LICENSE-APACHE Fill placeholder in LICENSE-APACHE file Oct 21, 2017
LICENSE-MIT Add LICENSE files Oct 13, 2017
README.md fix the readme Aug 9, 2018
appveyor.yml Surround rustup download with appveyor-retry Jan 14, 2018



Build Status Build status

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


Example usage

extern crate ctrlc;
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.

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


Licensed under either of


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.