Comment exécuter un script en fouction du journal
Bonjour,
J'aimerais faire un truc. J'ai un petit problème que mon Raspebrry boot sous Rasbian, il n'arrive pas démarrer un service.
Je peux le voir en exécutant cette commande
Code:
journalctl -u iot-lora-gateway.service --no-pager -n 1
S'il n'a pas démarré, la commande me retournera
Citation:
Oct 21 20:54:24 iotloragateway iot-lora-gateway[668]: 20:54:24 ERROR: [TTN] Connection to server "" failed, retry in 60 seconds
L'idée est de faire un script qui va être exécuté par cron toutes le 5mn et s'il lit le texte 'ERROR: [TTN] Connection to server "" failed', il va exécuter la commande
Code:
sudo systemctl restart iot-lora-gateway.service
Si non, on pourrait aussi analyser cette commande
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
sudo systemctl status iot-lora-gateway.service
● iot-lora-gateway.service - IOT LoRa Gateway Packet Forwarder
Loaded: loaded (/lib/systemd/system/iot-lora-gateway.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-10-21 20:53:53 BST; 15min ago
Process: 481 ExecStartPre=/opt/iotloragateway/iot-lora-gateway-reset.sh (code=exited, status=0/SUCCESS)
Main PID: 668 (iot-lora-gatewa)
Tasks: 1 (limit: 2065)
CGroup: /system.slice/iot-lora-gateway.service
└─668 /opt/iotloragateway/iot-lora-gateway
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 150, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 160, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 170, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 180, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 190, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 200, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 210, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 220, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: src/ttn_transport.c:371:ttn_connect(): ttn_connect: sleeping() at 230, total 24
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: 21:01:24 ERROR: [TTN] Connection to server "" failed, retry in 480 seconds |
et si cette commande affiche la dernière ligne que l'on voit
Citation:
Oct 21 21:01:24 iotloragateway iot-lora-gateway[668]: 21:01:24 ERROR: [TTN] Connection to server "" failed,
il exécute la commande
sudo systemctl restart iot-lora-gateway.service
Ce que je ne sais absolument pas faire, c'est comment analyser l'exécution d'une commande si la sortie contient une chaine de caractère, comme
Citation:
ERROR: [TTN] Connection to server "" failed
L'idée finale est d'exécuter ce script dès que la Raspberry a démarré jusqu'à ce qu'il lise ceci
Citation:
INFO: [TTN] send status success for
et s'il ne lit pas ceci, mais lis ceci
Citation:
ERROR: [TTN] Connection to server "" failed
il lance la commande
Code:
sudo systemctl restart iot-lora-gateway.service
Merci pour vos lumières