25 lines
846 B
Bash
25 lines
846 B
Bash
#!/bin/bash
|
|
|
|
# 0 9 * * * /home/web/scripts_linux/shutdown_weekends.sh >> /var/log/cron_shutdown_weekend.log 2>&1
|
|
|
|
# Establecer PATH para evitar problemas con cron
|
|
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
# Obtener la fecha actual y el día de la semana (0=Domingo, 6=Sábado)
|
|
d=$(date +"%d/%m/%Y %H:%M")
|
|
day_of_week=$(date +"%u") # 6 = Sábado, 7 = Domingo
|
|
|
|
# Captura el nombre del usuario que ejecutó el script (en cron será root)
|
|
usr=${SUDO_USER:-$(whoami)}
|
|
|
|
# Definir la ruta del log
|
|
LOG_FILE="/var/log/shutdown_weekend.log"
|
|
|
|
# Si es sábado (6) o domingo (7), apagar el equipo
|
|
if [[ "$day_of_week" -eq 6 || "$day_of_week" -eq 7 ]]; then
|
|
echo "$d: Shutdown ejecutado por $usr (Fin de semana)" >> "$LOG_FILE"
|
|
shutdown -h now
|
|
else
|
|
echo "$d: No es fin de semana, no se apaga el equipo." >> "$LOG_FILE"
|
|
fi
|