| Crates.io | shutdown-now |
| lib.rs | shutdown-now |
| version | 1.0.4 |
| created_at | 2025-10-19 16:34:31.296971+00 |
| updated_at | 2025-10-20 00:12:22.872418+00 |
| description | Gracefully handle shutdown and termination signals with zero hassle. |
| homepage | |
| repository | https://github.com/canmi21/shutdown-now |
| max_upload_size | |
| id | 1890595 |
| size | 32,983 |
Gracefully handle shutdown and termination signals with zero hassle.
This Rust crate provides a simple way to wait for shutdown signals (like Ctrl+C or SIGTERM) in asynchronous applications, making it easy to implement graceful shutdowns in servers or long-running tasks.
Add this to your Cargo.toml:
[dependencies]
shutdown-now = "1"
The crate exposes two main functions:
graceful(): An async function that waits for a shutdown signal and prints a message when received.graceful_future(): Returns a future that completes when a shutdown signal is received, allowing non-blocking awaits.use shutdown_now::graceful;
#[tokio::main]
async fn main() {
println!("App started, waiting for shutdown signal...");
graceful().await;
println!("Exiting gracefully...");
}
This will run until Ctrl+C is pressed or SIGTERM is sent, then print a shutdown message and exit.
For web servers like Axum, use it with with_graceful_shutdown:
use axum::{Router, routing::get};
use shutdown_now::graceful;
use std::net::SocketAddr;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "Hello, world" }));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).await.unwrap();
println!("Server started on {}", addr);
if let Err(err) = axum::serve(listener, app)
.with_graceful_shutdown(graceful())
.await
{
eprintln!("Server error: {}", err);
}
println!("Server has been shut down gracefully.");
}
This project is licensed under the MIT License - see the LICENSE file for details.
Check out the source code on GitHub.