commit be85567089429420a775c5c5098eebd54e7ccf4b Author: David Itehua Xalamihua Date: Tue Mar 11 18:35:54 2025 -0600 inicio del repositorio de scripts diff --git a/shutdown.sh b/shutdown.sh new file mode 100644 index 0000000..e2b513e --- /dev/null +++ b/shutdown.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# 0 21 * * * /home/web/scripts_linux/shutdown.sh >> /home/web/scripts_linux/cron.log 2>&1 +# 0 23 * * * /home/web/scripts_linux/shutdown.sh >> /home/web/scripts_linux/cron.log 2>&1 + +# sudo chmod +x /ruta/al/scripts.sh +# sudo chmod -R 777 /ruta/carpeta/scripts_linux/ + +# Establecer PATH para evitar problemas con cron +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +# Obtener la fecha actual +d=$(date +"%d/%m/%Y %H:%M") + +# Captura el nombre del usuario que ejecutó el script (en cron será root) +usr=${SUDO_USER:-$(whoami)} + +# Definir la ruta del log (guardar siempre en /var/log para evitar problemas) +LOG_FILE="/var/log/shutdown_history.log" + +# Registrar la ejecución en el log +echo "$d: Shutdown ejecutado por $usr" >> "$LOG_FILE" + +# Apagar el sistema +shutdown -h now + diff --git a/shutdown_weekends.sh b/shutdown_weekends.sh new file mode 100644 index 0000000..14322a1 --- /dev/null +++ b/shutdown_weekends.sh @@ -0,0 +1,24 @@ +#!/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 diff --git a/upgrade.sh b/upgrade.sh new file mode 100644 index 0000000..f0e2252 --- /dev/null +++ b/upgrade.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# 0 18 * * * /home/web/scripts_linux/upgrade.sh >> /home/web/scripts_linux/cron.log 2>&1 +# sudo chmod +x /ruta/al/scripts.sh +# sudo chmod -R 777 /home/web/scripts_linux/ + +# Establecer PATH para asegurarse de que los comandos se encuentran +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + +# Obtener la fecha actual +d=$(date +"%d/%m/%Y %H:%M") + +# Captura el nombre del usuario que ejecutó el script +usr=$(who | awk '{print $1}' | head -n 1) + +# Asegurar que el usuario no esté vacío, usar root como fallback +usr=${usr:-root} + +# Definir la ruta del log +LOG_FILE="/home/$usr/scripts_linux/history_log.txt" + +# Verificar si el directorio existe, si no, crearlo +mkdir -p "/home/$usr/scripts_linux" + +# Registrar la ejecución en el log +echo "$d: upgrade" >> "$LOG_FILE" + +# Ejecutar actualización del sistema +apt-get update >> "$LOG_FILE" 2>&1 +apt-get upgrade -y >> "$LOG_FILE" 2>&1