Files
MCPletA2A/stop.sh
2026-03-30 17:39:13 +09:00

67 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================================
# MCPletA2A — stop.sh
# Gracefully stops all managed services (SIGTERM → wait → SIGKILL).
# =============================================================================
set -uo pipefail
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_DIR="$BASE_DIR/.pids"
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'
CYAN='\033[0;36m'; BOLD='\033[1m'; RESET='\033[0m'
info() { echo -e "${CYAN}[stop]${RESET} $*"; }
ok() { echo -e "${GREEN}[stop]${RESET} $*"; }
warn() { echo -e "${YELLOW}[stop]${RESET} $*"; }
stop_service() {
local name="$1"
local pidfile="$PID_DIR/$name.pid"
if [[ ! -f "$pidfile" ]]; then
warn "$name: no pid file found (already stopped?)"
return
fi
local pid
pid=$(cat "$pidfile")
if ! kill -0 "$pid" 2>/dev/null; then
warn "$name: process $pid is not running (stale pid file removed)"
rm -f "$pidfile"
return
fi
info "Stopping $name (pid $pid) ..."
kill -TERM "$pid" 2>/dev/null || true
# Wait up to 5 s for graceful exit
local waited=0
while kill -0 "$pid" 2>/dev/null && [[ $waited -lt 10 ]]; do
sleep 0.5
waited=$((waited + 1))
done
if kill -0 "$pid" 2>/dev/null; then
warn "$name did not exit gracefully — sending SIGKILL"
kill -KILL "$pid" 2>/dev/null || true
fi
rm -f "$pidfile"
ok "$name stopped"
}
if [[ ! -d "$PID_DIR" ]]; then
warn "No .pids directory found — nothing to stop"
exit 0
fi
# Stop in reverse start order
stop_service "platform-host"
stop_service "mock-services"
echo ""
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${GREEN}${BOLD} MCPletA2A stopped${RESET}"
echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"