service-systemd.sh
Le passage obligé du produit embarqué : tout script Python devient un service supervisé, redémarré s'il meurt et relancé au boot. Le here-document écrit l'unité systemd en une commande, status et journalctl inclus.
Matériel requis
Une Arduino Portenta X8 (module ST STM32MP1 : quad Cortex-A7 Linux + double cœur Cortex-M4 temps réel, 8 Go eMMC, 2 Go LPDDR4, Wi-Fi/BT embarqués, option LTE). Aucun ; un script Python ou binaire à superviser.
Câblage
Rien à câbler. Service accessible en SSH sur la carte.
Fonctionnement
tee écrit l'unité (Restart=always, WantedBy multi-user), daemon-reload l'enregistre, enable --now l'active immédiatement. journalctl centralise les logs.
Exécution
Côté Linux : accès SSH (USB-C routed ou réseau) sur l'image Portenta Linux, scripts bash/Python. Côté temps réel : Arduino IDE avec le core « Arduino Mbed OS Portenta X8 Boards », cœur M7 applicatif, flash via mboot depuis Linux.
Variantes
Ajouter WatchdogSec + sd_notify pour un watchdog systemd, ou dépendre de network-online pour un service cloud.
service-systemd.sh
#!/bin/bash
# Installe un service systemd pour un script applicatif quelconque.
# Usage : ./service-systemd.sh /root/mon-app.py mon-app
set -e
APP="${1:?Usage: $0 /chemin/app nom-service}"
NOM="${2:?nom du service requis}"
sudo tee /etc/systemd/system/$NOM.service >/dev/null <<UNIT
[Unit]
Description=$NOM (Portenta X8)
After=network-online.target
[Service]
ExecStart=/usr/bin/python3 $APP
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
UNIT
sudo systemctl daemon-reload
sudo systemctl enable --now $NOM
sudo systemctl status $NOM --no-pager | head -8
echo "Logs : journalctl -u $NOM -f"