Loading...
Closes all database connections and cleans up resources when session ends
{
"hookConfig": {
"hooks": {
"stop": {
"script": "./.claude/hooks/database-connection-cleanup.sh"
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\necho \"🗄️ Starting database connection cleanup...\" >&2\n\n# PostgreSQL cleanup\nif pgrep postgres >/dev/null 2>&1; then\n echo \"🐘 PostgreSQL: Checking connections...\" >&2\n \n # Check if psql is available and we can connect\n if command -v psql &> /dev/null; then\n # Count active connections\n ACTIVE_CONNECTIONS=$(psql -t -c \"SELECT count(*) FROM pg_stat_activity WHERE state = 'active' AND pid <> pg_backend_pid();\" 2>/dev/null | xargs || echo \"0\")\n IDLE_CONNECTIONS=$(psql -t -c \"SELECT count(*) FROM pg_stat_activity WHERE state = 'idle' AND pid <> pg_backend_pid();\" 2>/dev/null | xargs || echo \"0\")\n \n echo \"📊 PostgreSQL: $ACTIVE_CONNECTIONS active, $IDLE_CONNECTIONS idle connections\" >&2\n \n # Terminate long-running idle connections (older than 5 minutes)\n if [ \"$IDLE_CONNECTIONS\" -gt 0 ]; then\n TERMINATED=$(psql -t -c \"SELECT count(*) FROM (SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle' AND state_change < now() - interval '5 minutes' AND pid <> pg_backend_pid()) t;\" 2>/dev/null | xargs || echo \"0\")\n if [ \"$TERMINATED\" -gt 0 ]; then\n echo \"✅ PostgreSQL: Terminated $TERMINATED idle connections\" >&2\n fi\n fi\n else\n echo \"⚠️ PostgreSQL running but psql not available\" >&2\n fi\nfi\n\n# MySQL cleanup\nif pgrep mysql >/dev/null 2>&1 || pgrep mysqld >/dev/null 2>&1; then\n echo \"🐬 MySQL: Checking connections...\" >&2\n \n if command -v mysql &> /dev/null; then\n PROCESSLIST=$(mysql -e \"SHOW PROCESSLIST;\" 2>/dev/null | grep -c \"Sleep\" || echo \"0\")\n TOTAL_CONNECTIONS=$(mysql -e \"SHOW PROCESSLIST;\" 2>/dev/null | wc -l || echo \"0\")\n echo \"📊 MySQL: $TOTAL_CONNECTIONS total connections, $PROCESSLIST sleeping\" >&2\n \n # Kill long-running sleeping connections (optional, requires PROCESS privilege)\n # mysql -e \"KILL CONNECTION_ID;\" 2>/dev/null || true\n else\n echo \"⚠️ MySQL running but mysql client not available\" >&2\n fi\nfi\n\n# MongoDB cleanup\nif pgrep mongod >/dev/null 2>&1; then\n echo \"🍃 MongoDB: Checking connections...\" >&2\n \n if command -v mongosh &> /dev/null; then\n CONNECTIONS=$(mongosh --quiet --eval \"db.currentOp().inprog.length\" 2>/dev/null || echo \"0\")\n echo \"📊 MongoDB: $CONNECTIONS active operations\" >&2\n elif command -v mongo &> /dev/null; then\n CONNECTIONS=$(mongo --quiet --eval \"db.currentOp().inprog.length\" 2>/dev/null || echo \"0\")\n echo \"📊 MongoDB: $CONNECTIONS active operations\" >&2\n else\n echo \"⚠️ MongoDB running but mongo client not available\" >&2\n fi\nfi\n\n# Redis cleanup\nif pgrep redis-server >/dev/null 2>&1; then\n echo \"📮 Redis: Checking connections...\" >&2\n \n if command -v redis-cli &> /dev/null; then\n CLIENT_COUNT=$(redis-cli CLIENT LIST 2>/dev/null | wc -l || echo \"0\")\n echo \"📊 Redis: $CLIENT_COUNT connected clients\" >&2\n \n # Optionally close idle clients\n # redis-cli CLIENT KILL TYPE normal SKIPME yes 2>/dev/null || true\n else\n echo \"⚠️ Redis running but redis-cli not available\" >&2\n fi\nfi\n\n# Check for database connection strings in environment\nif [ -f \".env\" ]; then\n DB_VARS=$(grep -E \"(DATABASE_URL|MONGODB_URI|REDIS_URL|POSTGRES|MYSQL)\" .env 2>/dev/null | wc -l || echo \"0\")\n if [ \"$DB_VARS\" -gt 0 ]; then\n echo \"📋 Found $DB_VARS database configuration variables in .env\" >&2\n fi\nfi\n\necho \"✅ Database connection cleanup completed\" >&2\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"stop": {
"script": "./.claude/hooks/database-connection-cleanup.sh"
}
}
}#!/usr/bin/env bash
echo "🗄️ Starting database connection cleanup..." >&2
# PostgreSQL cleanup
if pgrep postgres >/dev/null 2>&1; then
echo "🐘 PostgreSQL: Checking connections..." >&2
# Check if psql is available and we can connect
if command -v psql &> /dev/null; then
# Count active connections
ACTIVE_CONNECTIONS=$(psql -t -c "SELECT count(*) FROM pg_stat_activity WHERE state = 'active' AND pid <> pg_backend_pid();" 2>/dev/null | xargs || echo "0")
IDLE_CONNECTIONS=$(psql -t -c "SELECT count(*) FROM pg_stat_activity WHERE state = 'idle' AND pid <> pg_backend_pid();" 2>/dev/null | xargs || echo "0")
echo "📊 PostgreSQL: $ACTIVE_CONNECTIONS active, $IDLE_CONNECTIONS idle connections" >&2
# Terminate long-running idle connections (older than 5 minutes)
if [ "$IDLE_CONNECTIONS" -gt 0 ]; then
TERMINATED=$(psql -t -c "SELECT count(*) FROM (SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle' AND state_change < now() - interval '5 minutes' AND pid <> pg_backend_pid()) t;" 2>/dev/null | xargs || echo "0")
if [ "$TERMINATED" -gt 0 ]; then
echo "✅ PostgreSQL: Terminated $TERMINATED idle connections" >&2
fi
fi
else
echo "⚠️ PostgreSQL running but psql not available" >&2
fi
fi
# MySQL cleanup
if pgrep mysql >/dev/null 2>&1 || pgrep mysqld >/dev/null 2>&1; then
echo "🐬 MySQL: Checking connections..." >&2
if command -v mysql &> /dev/null; then
PROCESSLIST=$(mysql -e "SHOW PROCESSLIST;" 2>/dev/null | grep -c "Sleep" || echo "0")
TOTAL_CONNECTIONS=$(mysql -e "SHOW PROCESSLIST;" 2>/dev/null | wc -l || echo "0")
echo "📊 MySQL: $TOTAL_CONNECTIONS total connections, $PROCESSLIST sleeping" >&2
# Kill long-running sleeping connections (optional, requires PROCESS privilege)
# mysql -e "KILL CONNECTION_ID;" 2>/dev/null || true
else
echo "⚠️ MySQL running but mysql client not available" >&2
fi
fi
# MongoDB cleanup
if pgrep mongod >/dev/null 2>&1; then
echo "🍃 MongoDB: Checking connections..." >&2
if command -v mongosh &> /dev/null; then
CONNECTIONS=$(mongosh --quiet --eval "db.currentOp().inprog.length" 2>/dev/null || echo "0")
echo "📊 MongoDB: $CONNECTIONS active operations" >&2
elif command -v mongo &> /dev/null; then
CONNECTIONS=$(mongo --quiet --eval "db.currentOp().inprog.length" 2>/dev/null || echo "0")
echo "📊 MongoDB: $CONNECTIONS active operations" >&2
else
echo "⚠️ MongoDB running but mongo client not available" >&2
fi
fi
# Redis cleanup
if pgrep redis-server >/dev/null 2>&1; then
echo "📮 Redis: Checking connections..." >&2
if command -v redis-cli &> /dev/null; then
CLIENT_COUNT=$(redis-cli CLIENT LIST 2>/dev/null | wc -l || echo "0")
echo "📊 Redis: $CLIENT_COUNT connected clients" >&2
# Optionally close idle clients
# redis-cli CLIENT KILL TYPE normal SKIPME yes 2>/dev/null || true
else
echo "⚠️ Redis running but redis-cli not available" >&2
fi
fi
# Check for database connection strings in environment
if [ -f ".env" ]; then
DB_VARS=$(grep -E "(DATABASE_URL|MONGODB_URI|REDIS_URL|POSTGRES|MYSQL)" .env 2>/dev/null | wc -l || echo "0")
if [ "$DB_VARS" -gt 0 ]; then
echo "📋 Found $DB_VARS database configuration variables in .env" >&2
fi
fi
echo "✅ Database connection cleanup completed" >&2
exit 0PostgreSQL connections not terminated despite hook execution
Verify psql client is in PATH and can connect without password. Check pg_hba.conf allows local connections. Ensure user has permission to query pg_stat_activity and call pg_terminate_backend.
Stop hook runs but database processes still detected
Hook reports connections but doesn't stop database servers. Use pgrep to verify process detection works. Check connection cleanup SQL executes successfully. Monitor stderr for command errors.
Idle connection count shows zero despite active sessions
Check state filter in pg_stat_activity query matches your PostgreSQL version. Verify 5-minute threshold is appropriate for your workflow. Use psql directly to debug query results.
MySQL processlist command fails with access denied
Grant PROCESS privilege to MySQL user. Run GRANT PROCESS ON *.* TO 'user'@'localhost'. Verify mysql client connects with correct credentials from .env or environment variables.
MongoDB connection check fails with authentication error
Use mongosh for MongoDB 5+ or mongo for older versions. Configure connection string with credentials. Check authentication database matches user creation. Verify network access if using remote MongoDB.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters