Files
ascoetpi/automated_shutdown/read.md

2.2 KiB
Raw Blame History

An automated shutdown solution based on the detection of a prolonged ethernet connection loss. This is because AscoetPi is connection to the router through a switch which is programmed to shutdown at night.

Crée un fichier /etc/systemd/system/eth-monitor.service :

sudo nano /etc/systemd/system/eth-monitor.service

Colle ce contenu :

[Unit] Description=Ethernet Connection Monitor After=network.target [Service] Type=simple ExecStart=/usr/local/bin/check_eth_connection.sh Restart=on-failure RestartSec=60 TimeoutStopSec=5 [Install] WantedBy=multi-user.target

c. Créer le timer systemd pour déclencher lextinction Crée un fichier /etc/systemd/system/eth-monitor.timer :

sudo nano /etc/systemd/system/eth-monitor.timer

Colle ce contenu :

[Unit] Description=Timer for Ethernet Connection Monitor

[Timer] OnBootSec=5min OnUnitActiveSec=1min AccuracySec=1s

[Install] WantedBy=timers.target

d. Créer un service pour éteindre après 5 minutes de panne Crée un fichier /etc/systemd/system/shutdown-on-eth-fail.service :

sudo nano /etc/systemd/system/shutdown-on-eth-fail.service

Colle ce contenu :

[Unit] Description=Shutdown on Ethernet Failure After=eth-monitor.service

[Service] Type=oneshot ExecStart=/bin/sh -c 'if [ $(journalctl -u eth-monitor.service --since "5 minutes ago" | grep -c "is down") -ge 5 ]; then /sbin/shutdown -h now; fi'

[Install] WantedBy=multi-user.target

e. Activer et démarrer les services

sudo systemctl daemon-reload sudo systemctl enable eth-monitor.service sudo systemctl enable eth-monitor.timer sudo systemctl enable shutdown-on-eth-fail.service sudo systemctl start eth-monitor.timer sudo systemctl start eth-monitor.service

  1. Vérifier le bon fonctionnement

Consulte les logs :

journalctl -u eth-monitor.service -f

Teste en débranchant le câble Ethernet et attends 5 minutes.

  1. Explications

eth-monitor.service : Vérifie létat de linterface Ethernet toutes les minutes. eth-monitor.timer : Déclenche la vérification régulièrement. shutdown-on-eth-fail.service : Compte le nombre de pannes sur les 5 dernières minutes et éteint si nécessaire.