Files
tokio/tokio-signal/examples/sighup-example.rs
T

42 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2019-07-03 10:40:59 -07:00
#![feature(async_await)]
2018-09-02 11:32:50 +02:00
// A trick to not fail build on non-unix platforms when using unix-specific features.
#[cfg(unix)]
mod platform {
2019-07-03 10:40:59 -07:00
use futures_util::stream::StreamExt;
2018-09-02 11:32:50 +02:00
use tokio_signal::unix::{Signal, SIGHUP};
pub async fn main() {
2018-09-02 11:32:50 +02:00
// on Unix, we can listen to whatever signal we want, in this case: SIGHUP
let mut stream = Signal::new(SIGHUP).await.unwrap();
2018-09-02 11:32:50 +02:00
println!("Waiting for SIGHUPS (Ctrl+C to quit)");
println!(
" TIP: use `pkill -sighup sighup-example` from a second terminal \
2019-02-21 11:56:15 -08:00
to send a SIGHUP to all processes named 'sighup-example' \
(i.e. this binary)"
2018-09-02 11:32:50 +02:00
);
// Up until now, we haven't really DONE anything, just prepared
// our futures, now it's time to actually await the results!
while let Some(the_signal) = stream.next().await {
2018-09-02 11:32:50 +02:00
println!(
"*Got signal {:#x}* I should probably reload my config \
2019-02-21 11:56:15 -08:00
or something",
the_signal
2018-09-02 11:32:50 +02:00
);
}
2018-09-02 11:32:50 +02:00
}
}
#[cfg(not(unix))]
mod platform {
pub async fn main() {}
2018-09-02 11:32:50 +02:00
}
2019-07-03 10:40:59 -07:00
#[tokio::main]
async fn main() {
2019-07-03 10:40:59 -07:00
platform::main().await
}