Files
apagado_seguro/scripts/shutdown-watcher.sh
T
2026-07-21 12:37:30 +02:00

40 lines
1.6 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CONFIGURACION ---------------------------------------------------------------
SIGNAL_DIR="/opt/shutdown-signal"
SHUTDOWN_ORCHESTRATOR="/usr/local/sbin/orchestrate-shutdown.sh"
TOTAL_SHUTDOWN_ORCHESTRATOR="/usr/local/sbin/orchestrate-total-shutdown.sh"
STARTUP_ORCHESTRATOR="/usr/local/sbin/orchestrate-startup.sh"
LOG="/var/log/safe-shutdown.log"
# PROGRAMA --------------------------------------------------------------------
log() { echo "$(date -Is) [watcher] $*" | tee -a "$LOG"; }
log "watcher iniciado, vigilando $SIGNAL_DIR/"
while true; do
inotifywait -q -e create -e moved_to "$SIGNAL_DIR" |
while read -r dir event file; do
case "$file" in
apagar)
log "señal detectada ($file). Lanzando secuencia de apagado."
"$SHUTDOWN_ORCHESTRATOR"
rm -f "$SIGNAL_DIR/apagar"
log "señal consumida y borrada."
;;
encender)
log "señal detectada ($file). Lanzando secuencia de encendido."
"$STARTUP_ORCHESTRATOR"
rm -f "$SIGNAL_DIR/encender"
log "señal consumida y borrada."
;;
apagado_total)
log "señal detectada ($file). Lanzando secuencia de apagado TOTAL."
"$TOTAL_SHUTDOWN_ORCHESTRATOR"
rm -f "$SIGNAL_DIR/apagado_total"
log "señal consumida y borrada."
;;
esac
done
done