Files
agents_and_robots/dev-scripts/stop.sh
T

60 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# stop.sh — detiene uno o todos los agentes en ejecución
#
# Uso:
# ./dev-scripts/stop.sh # detiene todos los que estén corriendo
# ./dev-scripts/stop.sh assistant-bot # detiene uno específico
source "$(dirname "$0")/_common.sh"
TARGET="${1:-}"
stopped=0
while IFS='|' read -r id _version _enabled _desc _cfg; do
[[ -n "$TARGET" && "$id" != "$TARGET" ]] && continue
if ! is_running "$id"; then
dim " $id (no está corriendo)"
continue
fi
# Kill ALL instances, not just the one in the PID file
all_pids="$(find_agent_pids "$id")"
instance_count="$(echo "$all_pids" | grep -c . 2>/dev/null || echo 0)"
if [[ "$instance_count" -gt 1 ]]; then
warn "$id has $instance_count instances running — stopping all"
fi
# Send SIGTERM to all instances
for p in $all_pids; do
kill -TERM "$p" 2>/dev/null || true
done
# Wait up to 5s for graceful shutdown
for _ in {1..10}; do
remaining="$(find_agent_pids "$id")"
[[ -z "$remaining" ]] && break
sleep 0.5
done
# SIGKILL any survivors
survivors="$(find_agent_pids "$id")"
if [[ -n "$survivors" ]]; then
warn "$id no respondió a SIGTERM, enviando SIGKILL..."
for p in $survivors; do
kill -9 "$p" 2>/dev/null || true
done
fi
rm -f "$(pid_file "$id")"
ok "$id detenido ($instance_count instance(s) stopped)"
((stopped++)) || true
done < <(list_agents_raw)
[[ "$stopped" -eq 0 && -z "$TARGET" ]] && dim "Ningún agente estaba corriendo."
[[ -n "$TARGET" && "$stopped" -eq 0 ]] && fail "Agente '$TARGET' no encontrado o no estaba corriendo."
true