graceful-shutdown▌
aj-geddes/useful-ai-prompts · updated Apr 8, 2026
Implement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.
Graceful Shutdown
Table of Contents
Overview
Implement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.
When to Use
- Kubernetes/Docker deployments
- Rolling updates and deployments
- Server restarts
- Load balancer drain periods
- Zero-downtime deployments
- Process managers (PM2, systemd)
- Long-running background jobs
- Database connection cleanup
Quick Start
Minimal working example:
import express from "express";
import http from "http";
class GracefulShutdownServer {
private app: express.Application;
private server: http.Server;
private isShuttingDown = false;
private activeConnections = new Set<any>();
private shutdownTimeout = 30000; // 30 seconds
constructor() {
this.app = express();
this.server = http.createServer(this.app);
this.setupMiddleware();
this.setupRoutes();
this.setupShutdownHandlers();
}
private setupMiddleware(): void {
// Track active connections
this.app.use((req, res, next) => {
if (this.isShuttingDown) {
res.set("Connection", "close");
return res.status(503).json({
error: "Server is shutting down",
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Express.js Graceful Shutdown | Express.js Graceful Shutdown |
| Kubernetes-Aware Shutdown | Kubernetes-Aware Shutdown |
| Worker Process Shutdown | Worker Process Shutdown |
| Database Connection Pool Shutdown | Database Connection Pool Shutdown |
| PM2 Graceful Shutdown | PM2 Graceful Shutdown |
| Python/Flask Graceful Shutdown | Python/Flask Graceful Shutdown |
Best Practices
✅ DO
- Handle SIGTERM and SIGINT signals
- Stop accepting new requests immediately
- Wait for in-flight requests to complete
- Set reasonable shutdown timeouts
- Close database connections properly
- Flush logs and metrics
- Fail health checks during shutdown
- Test shutdown procedures
- Log shutdown progress
- Use graceful shutdown in containers
❌ DON'T
- Ignore shutdown signals
- Force kill processes without cleanup
- Set unreasonably long timeouts
- Skip resource cleanup
- Forget to close connections
- Block shutdown indefinitely
Discussion
Product Hunt–style comments (not star reviews)- No comments yet — start the thread.
Ratings
4.7★★★★★66 reviews- ★★★★★Amina Mensah· Dec 28, 2024
Registry listing for graceful-shutdown matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Dhruvi Jain· Dec 20, 2024
graceful-shutdown reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Isabella Thompson· Dec 20, 2024
Registry listing for graceful-shutdown matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Michael Lopez· Dec 16, 2024
I recommend graceful-shutdown for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Olivia Bansal· Dec 12, 2024
graceful-shutdown fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.
- ★★★★★Harper Huang· Dec 12, 2024
graceful-shutdown reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Olivia Ghosh· Dec 8, 2024
Solid pick for teams standardizing on skills: graceful-shutdown is focused, and the summary matches what you get after install.
- ★★★★★Isabella Robinson· Dec 4, 2024
Keeps context tight: graceful-shutdown is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Isabella Bansal· Nov 27, 2024
We added graceful-shutdown from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.
- ★★★★★Amina Kim· Nov 19, 2024
Useful defaults in graceful-shutdown — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.
showing 1-10 of 66