| 1 |
# CtrlC |
| 2 |
A simple easy to use wrapper around Ctrl-C signal. |
| 3 |
|
| 4 |
[Documentation](http://detegr.github.io/doc/ctrlc/) |
| 5 |
|
| 6 |
## Example usage |
| 7 |
|
| 8 |
In `cargo.toml`: |
| 9 |
|
| 10 |
```toml |
| 11 |
[dependencies] |
| 12 |
ctrlc = "3.5" |
| 13 |
``` |
| 14 |
|
| 15 |
then, in `main.rs` |
| 16 |
|
| 17 |
```rust |
| 18 |
use std::sync::mpsc::channel; |
| 19 |
use ctrlc; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
let (tx, rx) = channel(); |
| 23 |
|
| 24 |
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) |
| 25 |
.expect("Error setting Ctrl-C handler"); |
| 26 |
|
| 27 |
println!("Waiting for Ctrl-C..."); |
| 28 |
rx.recv().expect("Could not receive from channel."); |
| 29 |
println!("Got it! Exiting..."); |
| 30 |
} |
| 31 |
``` |
| 32 |
|
| 33 |
#### Try the example yourself |
| 34 |
`cargo build --examples && target/debug/examples/readme_example` |
| 35 |
|
| 36 |
## Handling SIGTERM and SIGHUP |
| 37 |
Add CtrlC to Cargo.toml using `termination` feature and CtrlC will handle SIGINT, SIGTERM and SIGHUP. |
| 38 |
|
| 39 |
## License |
| 40 |
|
| 41 |
Licensed under either of |
| 42 |
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 43 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 44 |
at your option. |
| 45 |
|
| 46 |
### Contribution |
| 47 |
|
| 48 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 49 |
for inclusion in the work by you shall be dual licensed as above, without any |
| 50 |
additional terms or conditions. |
| 51 |
|
| 52 |
## Similar crates |
| 53 |
|
| 54 |
There are alternatives that give you more control over the different signals and/or add async support. |
| 55 |
|
| 56 |
- [signal-hook](https://github.com/vorner/signal-hook) |
| 57 |
- [tokio::signal](https://docs.rs/tokio/latest/tokio/signal/index.html) |