new rebooters.sh

This commit is contained in:
Script Proyect by HP3ICC 2024-11-18 02:49:53 +00:00
parent 0c86714bc6
commit 3cf054aaa0
1 changed files with 123 additions and 107 deletions

View File

@ -1,107 +1,123 @@
#!/bin/bash
SCRIPT_NAME="rebooter.sh"
# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
#####################################################################################################################
# rebooter general system
#####################################################################################################################
sudo cat > /usr/local/bin/rebooter1.sh <<- "EOF"
#!/bin/bash
# Tiempo de monitoreo en segundos
TIEMPO_MONITOREO=300
# Tiempo de espera entre los chequeos en segundos
TIEMPO_ESPERA=60
# Función para verificar el estado de Internet
verificar_internet() {
ping -c 5 noip.com > /dev/null 2>&1
return $?
}
# Bucle infinito para monitorear
while true; do
if verificar_internet; then
# Acción silenciosa
:
echo "internet ok"
else
# echo "sin internet"
sleep $TIEMPO_MONITOREO
if verificar_internet; then
# Conexión restablecida
# echo "internet restablecido"
:
else
sudo reboot
# echo "apliando reboot"
fi
fi
sleep $TIEMPO_ESPERA
done
EOF
#
#####################################################################################################################
# rebooter 2
#####################################################################################################################
sudo cat > /usr/local/bin/rebooter2.sh <<- "EOF"
#!/bin/bash
sleep 300
while :
do
SERVER=noip.com
ping -c5 ${SERVER} > /dev/null
if [ $? != 0 ]
then
#
sudo reboot
fi
sleep 60
done
EOF
#
sudo cat > /lib/systemd/system/rebooter1.service <<- "EOF"
[Unit]
Description=Rebooter Service1
After=network.target
[Service]
ExecStart=/usr/local/bin/rebooter1.sh
Restart=on-failure
[Install]
WantedBy=default.target
EOF
#
sudo cat > /lib/systemd/system/rebooter2.service <<- "EOF"
[Unit]
Description=Rebooter Service2
After=network.target
[Service]
ExecStart=/usr/local/bin/rebooter2.sh
Restart=on-failure
[Install]
WantedBy=default.target
EOF
#########
sudo systemctl daemon-reload
sudo chmod +x /usr/local/bin/rebooter*
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
#!/bin/bash
SCRIPT_NAME="rebooter.sh"
# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
#####################################################################################################################
# rebooter general system
#####################################################################################################################
sudo cat > /usr/local/bin/rebooter1.sh <<- "EOF"
#!/bin/bash
# Tiempo de espera inicial en segundos (5 minutos)
TIEMPO_INICIO=300
# Tiempo de espera entre los chequeos en segundos (3 minutos)
TIEMPO_ESPERA=180
# Número máximo de intentos fallidos consecutivos antes de reiniciar
MAX_INTENTOS_FALLIDOS=3
# Contador de intentos fallidos consecutivos
intentos_fallidos=0
# Lista de sitios a hacer ping (puedes agregar más sitios aquí)
SITIOS_PING=("noip.com" "google.com" "cloudflare.com")
# Función para verificar el estado de Internet
verificar_internet() {
# Intentamos hacer ping a cada sitio en la lista
for sitio in "${SITIOS_PING[@]}"; do
ping -c 1 -W 1 $sitio > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Si uno de los sitios responde, consideramos que hay conexión
return 0
fi
done
# Si ningún sitio responde, consideramos que no hay conexión
return 1
}
# Espera inicial de 5 minutos antes de comenzar las pruebas
sleep $TIEMPO_INICIO
# Bucle infinito para monitorear
while true; do
if verificar_internet; then
# Si la conexión está activa, reseteamos el contador de fallos
intentos_fallidos=0
else
# Si no hay conexión, incrementamos el contador de intentos fallidos
intentos_fallidos=$((intentos_fallidos + 1))
# Si se han alcanzado los intentos fallidos consecutivos, reiniciamos el sistema
if [ $intentos_fallidos -ge $MAX_INTENTOS_FALLIDOS ]; then
echo "Conexión perdida por 3 intentos consecutivos. Reiniciando el sistema..."
sudo reboot
fi
fi
# Esperar antes de volver a comprobar la conexión (3 minutos)
sleep $TIEMPO_ESPERA
done
EOF
#
#####################################################################################################################
# rebooter 2
#####################################################################################################################
sudo cat > /usr/local/bin/rebooter2.sh <<- "EOF"
#!/bin/bash
sleep 300
while :
do
SERVER=noip.com
ping -c5 ${SERVER} > /dev/null
if [ $? != 0 ]
then
#
sudo reboot
fi
sleep 60
done
EOF
#
sudo cat > /lib/systemd/system/rebooter1.service <<- "EOF"
[Unit]
Description=Rebooter Service1
After=network.target
[Service]
ExecStart=/usr/local/bin/rebooter1.sh
Restart=on-failure
[Install]
WantedBy=default.target
EOF
#
sudo cat > /lib/systemd/system/rebooter2.service <<- "EOF"
[Unit]
Description=Rebooter Service2
After=network.target
[Service]
ExecStart=/usr/local/bin/rebooter2.sh
Restart=on-failure
[Install]
WantedBy=default.target
EOF
#########
sudo systemctl daemon-reload
sudo chmod +x /usr/local/bin/rebooter*
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt