Une question qu'on m'a posé sur une autre ml:

> Cela fait un bout de temps que j'essaie de trouver une solution
> pour voir un code d'erreur d'une commande qui est lancée en arrière
> plan ?

hum.

   schaefer@defian:~% echo -e '#! /bin/sh\nexit 42' > /tmp/error_script.sh \\
                      && chmod a+rx /tmp/error_script.sh
   schaefer@defian:~% /tmp/error_script.sh; echo $?
   42

en arrière-plan:

   schaefer@defian:~% (/tmp/error_script.sh &); echo $?
   0

effectivement, ça marche pas et c'est normal: il faut introduire une
synchronicité (pardon):

   schaefer@defian:~% help wait
   wait: wait [n]
     Wait for the specified process and report its termination status.  If
     N is not given, all currently active child processes are waited for,
     and the return code is zero.  N may be a process ID or a job
     specification; if a job spec is given, all processes in the job's
     pipeline are waited for.

Donc il faut spécifier le processus attendu en paramètre. Cet identifiant
doit être stocké dans une variable juste après le lancement de la commande
concernée en parallèle (ou du moins avant le lancement d'une autre). On le
trouve dans: $!

Je n'ai pas vraiment cherché, mais à voir ce truc ne marche bien que dans
des scripts, pas interactivement.

Il marche dans ce script:

   schaefer@defian:~% more /tmp/launcher_script.sh 
   #! /bin/sh

   /tmp/error_script.sh &
   THE_PID=$!

   echo PID was $THE_PID
   wait $THE_PID
   echo $?

Résultat:
   schaefer@defian:~% sh /tmp/launcher_script.sh 
   PID was 1237
   42


--
http://www-internal.alphanet.ch/linux-leman/ avant de poser
une question. Ouais, pour se désabonner aussi.

Répondre à