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
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: aj-geddes-useful-ai-prompts-graceful-shutdown3description: Graceful Shutdown4---56# Graceful Shutdown78## Table of Contents910- [Overview](#overview)11- [When to Use](#when-to-use)12- [Quick Start](#quick-start)13- [Reference Guides](#reference-guides)14- [Best Practices](#best-practices)1516## Overview1718Implement proper shutdown procedures to ensure all requests are completed, connections are closed, and resources are released before process termination.1920## When to Use2122- Kubernetes/Docker deployments23- Rolling updates and deployments24- Server restarts25- Load balancer drain periods26- Zero-downtime deployments27- Process managers (PM2, systemd)28- Long-running background jobs29- Database connection cleanup3031## Quick Start3233Minimal working example:3435```typescript36import express from "express";37import http from "http";3839class GracefulShutdownServer {40 private app: express.Application;41 private server: http.Server;42 private isShuttingDown = false;43 private activeConnections = new Set<any>();44 private shutdownTimeout = 30000; // 30 seconds4546 constructor() {47 this.app = express();48 this.server = http.createServer(this.app);49 this.setupMiddleware();50 this.setupRoutes();51 this.setupShutdownHandlers();52 }5354 private setupMiddleware(): void {55 // Track active connections56 this.app.use((req, res, next) => {57 if (this.isShuttingDown) {58 res.set("Connection", "close");59 return res.status(503).json({60 error: "Server is shutting down",61// ... (see reference guides for full implementation)62```6364## Reference Guides6566Detailed implementations in the `references/` directory:6768| Guide | Contents |69|---|---|70| [Express.js Graceful Shutdown](references/expressjs-graceful-shutdown.md) | Express.js Graceful Shutdown |71| [Kubernetes-Aware Shutdown](references/kubernetes-aware-shutdown.md) | Kubernetes-Aware Shutdown |72| [Worker Process Shutdown](references/worker-process-shutdown.md) | Worker Process Shutdown |73| [Database Connection Pool Shutdown](references/database-connection-pool-shutdown.md) | Database Connection Pool Shutdown |74| [PM2 Graceful Shutdown](references/pm2-graceful-shutdown.md) | PM2 Graceful Shutdown |75| [Python/Flask Graceful Shutdown](references/pythonflask-graceful-shutdown.md) | Python/Flask Graceful Shutdown |7677## Best Practices7879### ✅ DO8081- Handle SIGTERM and SIGINT signals82- Stop accepting new requests immediately83- Wait for in-flight requests to complete84- Set reasonable shutdown timeouts85- Close database connections properly86- Flush logs and metrics87- Fail health checks during shutdown88- Test shutdown procedures89- Log shutdown progress90- Use graceful shutdown in containers9192### ❌ DON'T9394- Ignore shutdown signals95- Force kill processes without cleanup96- Set unreasonably long timeouts97- Skip resource cleanup98- Forget to close connections99- Block shutdown indefinitely100101---102> Converted and distributed by [TomeVault](https://tomevault.io/claim/aj-geddes) — claim your Tome and manage your conversions.103<!-- tomevault:4.0:skill_md:2026-04-11 -->
Run npx skillmds@latest add tomevault-io/aj-geddes-useful-ai-prompts-graceful-shutdown in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Graceful Shutdown It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
tomevault-io (@tomevault-io) published this skill. Their other Agent Skills are listed on their SkillMD profile.