service-systemd.sh

Arduino Portenta X8 · Archivo : service-systemd.sh · Lenguaje : Bash · Uso : Créer un service systemd qui démarre l'application au boot

El paso obligatorio del producto embebido: cualquier script Python se vuelve servicio supervisado, reanudado si muere y relanzado en el arranque. El here-document escribe la unidad systemd en una orden, con status y journalctl incluidos.

Hardware

Un Arduino Portenta X8 (módulo ST STM32MP1: quad Cortex-A7 Linux + doble núcleo Cortex-M4 de tiempo real, 8 GB eMMC, 2 GB LPDDR4, Wi-Fi/BT integrados, opción LTE). Ninguno; un script Python o binario a supervisar.

Cableado

Nada que cablear. Servicio accesible por SSH en la placa.

Funcionamiento

tee escribe la unidad (Restart=always, WantedBy multi-user), daemon-reload la registra, enable --now la activa al momento. journalctl centraliza los logs.

Ejecución

Lado Linux: acceso SSH (USB-C routed o red) sobre la imagen Portenta Linux, scripts bash/Python. Lado tiempo real: Arduino IDE con el core « Arduino Mbed OS Portenta X8 Boards », núcleo M7 de aplicación, grabado vía mboot desde Linux.

Variantes

Añadir WatchdogSec + sd_notify para un watchdog systemd, o depender de network-online para un servicio 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"

← Volver a la ficha