🛠 runShellCommandOnLinux : faire exécuter des commandes Linux depuis le sketch
Objectives
The Yún's superpower: the microcontroller drives a real Linux system. The Process library and its runShellCommandOnLinux method launch a shell command on the AR9331 and return the output to the sketch, line by line. Typical use cases: testing connectivity (ping), reading the system time, querying system sensors — everything an 8-bit microcontroller cannot do.
Hardware
- 1 Arduino Yún connected to your network (see the WiFi configuration tutorial)
- PC with Serial Monitor (USB) to view the results
- No wiring required: everything happens between the two processors
Complete Code
// Lancer « ping » côté Linux et afficher le résultat dans le sketch.
#include <Bridge.h>
#include <Process.h>
void setup {
Bridge.begin;
Console.begin;
while (!Console) { } // patiente sur certains environnements
// 1) Commande simple : connaître l'uptime de Linux
Process uptime;
uptime.runShellCommandOnLinux("uptime");
Console.println(F("--- uptime ---"));
while (uptime.available) {
String ligne = uptime.readStringUntil('\n');
Console.println(ligne);
}
// 2) Test de connectivité : ping 1 fois google.com, analyse du résultat
Process ping;
ping.begin("ping");
ping.addParameter("-c");
ping.addParameter("1");
ping.addParameter("google.com");
ping.runShellCommandOnLinux(ping);
String sortie = ping.readString;
bool enLigne = sortie.indexOf("bytes from") >= 0;
Console.println(enLigne ? F("INTERNET: OK") : F("INTERNET: KO"));
// 3) La valeur de retour d'une commande shell (code de sortie)
Process test;
int rc = test.runShellCommandOnLinux("ping -c 1 -W 2 1.1.1.1");
Console.print(F("code retour ping 1.1.1.1 = ")); Console.println(rc);
}
void loop { }
Expected Verification
The Monitor displays the Linino OS uptime line (load averages), "INTERNET: OK" if the board has internet access, and the shell return code (0 = success, non-zero = failure — the Unix convention is passed directly). Unplug the Ethernet cable / turn off WiFi: the ping status changes to KO and rc becomes non-zero. You now have a microcontroller that "thinks" using OS tools.
Common Errors
- Expecting real-time ping output:
runShellCommandOnLinuxwaits for the command to FINISH; for streaming, userunShellCommand+ readingavailablein a loop. - Forgetting to read the output: if you do not consume
available/readString, the buffer fills up and the command may block. - Command injection: never concatenate input (sensor, HTTP request) into a string passed to
runShellCommandOnLinuxwithout validation — this is a real shell on a real OS. - Believing the command runs on the 32U4: it runs on Linux; the 32U4, meanwhile, remains blocked in
setupduring execution. - Unavailable commands: Linino OS is a minimal OpenWrt (busybox); some common binaries (curl depending on versions, full python) are not installed by default — install them with
opkg(see the advanced tutorial). - Return codes:
runShellCommandOnLinuxreturns the integer at the end of the command; a -1 or a timeout often means the Bridge has lost Linux (reboot, panic) — regain control withBridge.connected.
Other tutorials for this board
- Premiers pas sur la Yún : Bridge, Console et LED
- Connecter la Yún au WiFi et piloter une broche depuis le navigateur
- Envoyer un capteur analogique sur le réseau : potentiomètre et Bridge REST
- L'API REST du Bridge en détail : /arduino, /data et routes personnalisées
- Alerte email : bouton sur la Yún et envoi via sendmail
- Serveur web embarqué : YunServer, YunClient et pages dynamiques
Code examples for this board
clignotant-rest.inoArduinoconsole-bridge.inoArduinovaleurs-partagees.inoArduinoprocess-shell.inoArduinoserveur-json.inoArduinolecture-fichier.inoArduino