#!/bin/bash # Foxhunt HFT Trading System - Complete System Shutdown # Stops 3 standalone services + databases as per TLI_PLAN.md architecture RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' echo -e "${RED}๐Ÿ›‘ Stopping Foxhunt HFT Trading System${NC}" echo "=================================================" echo "Shutting down: 3 Services + Docker Databases" # Step 1: Stop services by PID files if they exist echo -e "${BLUE}๐Ÿ”Œ Stopping standalone services...${NC}" if [ -f .trading_service.pid ]; then TRADING_PID=$(cat .trading_service.pid) echo "๐Ÿ“ˆ Stopping Trading Service (PID: $TRADING_PID)" kill $TRADING_PID 2>/dev/null || true rm -f .trading_service.pid fi if [ -f .backtesting_service.pid ]; then BACKTEST_PID=$(cat .backtesting_service.pid) echo "๐Ÿ”„ Stopping Backtesting Service (PID: $BACKTEST_PID)" kill $BACKTEST_PID 2>/dev/null || true rm -f .backtesting_service.pid fi if [ -f .ml_training_service.pid ]; then ML_PID=$(cat .ml_training_service.pid) echo "๐Ÿง  Stopping ML Training Service (PID: $ML_PID)" kill $ML_PID 2>/dev/null || true rm -f .ml_training_service.pid fi # Step 2: Fallback - kill by process name echo -e "${YELLOW}๐Ÿงน Cleaning up any remaining service processes...${NC}" pkill -f "trading_service" || true pkill -f "backtesting_service" || true pkill -f "ml_training_service" || true # Give processes time to shut down gracefully sleep 2 # Step 3: Stop Docker databases echo -e "${BLUE}๐Ÿ—„๏ธ Stopping Docker databases...${NC}" # Use docker compose (newer) or docker-compose (older) DOCKER_COMPOSE_CMD="docker compose" if ! command -v docker >/dev/null 2>&1 || ! docker compose version >/dev/null 2>&1; then DOCKER_COMPOSE_CMD="docker-compose" fi if command -v docker >/dev/null 2>&1; then echo "Stopping databases with $DOCKER_COMPOSE_CMD..." $DOCKER_COMPOSE_CMD down # Optional: Remove volumes (uncomment to completely clean databases) # echo -e "${YELLOW}โš ๏ธ Removing database volumes (data will be lost)...${NC}" # docker volume rm foxhunt_postgres_data foxhunt_influxdb_data foxhunt_redis_data 2>/dev/null || true else echo -e "${YELLOW}โš ๏ธ Docker not found, skipping database shutdown${NC}" fi # Step 4: Clean up any remaining ports echo -e "${YELLOW}๐Ÿ” Checking for services still using ports...${NC}" for port in 50051 50052 50053; do PID=$(lsof -ti :$port 2>/dev/null || true) if [ ! -z "$PID" ]; then echo "โš ๏ธ Force killing process $PID on port $port" kill -9 $PID 2>/dev/null || true fi done echo -e "${GREEN}โœ… All services and databases stopped${NC}" echo "" echo "System components stopped:" echo "โ”œโ”€โ”€ Trading Service (port 50051)" echo "โ”œโ”€โ”€ Backtesting Service (port 50052)" echo "โ”œโ”€โ”€ ML Training Service (port 50053)" echo "โ””โ”€โ”€ Databases (PostgreSQL, InfluxDB, Redis)" echo "" echo -e "${GREEN}System shutdown complete${NC}"