send_ctrlc 0.1.3

A cross platform crate for sending ctrl-c to child processes
Documentation

send_ctrlc

Crate Docs Build codecov

A cross platform crate for sending ctrl-c to child processes

Install

cargo add send_ctrlc
# Or for async/tokio:
cargo add send_ctrlc -F tokio

Features

  • Cross platform (including Windows)
  • Uniform cross platform API
  • Both sync and async
  • Minimal dependencies:
    • Synchronous: libc on unix, and windows-sys on windows
    • Asynchronous: tokio (with only process feature)

Examples

use send_ctrlc::{Interruptable as _};

// Synchronous...

#[cfg(not(feature = "tokio"))]
fn main() {
        // Start a continuous ping using our special command constructor function
        let mut command = send_ctrlc::new_command("ping");
        #[cfg(windows)]
        command.arg("-t");
        command.arg("127.0.0.1");

        // Spawn the ping, interrupt it, and wait for it to complete
        let mut child = command.spawn().unwrap();
        child.send_ctrl_c().unwrap();
        child.wait().unwrap();
}

// or asynchronous...

#[cfg(feature = "tokio")]
#[tokio::main]
async fn main() {
        // Start a continuous ping using our special command constructor function
        let mut command = send_ctrlc::new_tokio_command("ping");
        #[cfg(windows)]
        command.arg("-t");
        command.arg("127.0.0.1");

        // Spawn the ping, interrupt it, and wait for it to complete
        let mut child = command.spawn().unwrap();
        child.send_ctrl_c().unwrap();
        child.wait().await.unwrap();
}

Contributions

Contributions are welcome as long they align with the vision for this crate.