Score:0

How to run several shell scripts but terminate all of them if one encounters an error / does not terminate successfully?

jp flag

I have a shell script that runs several other shell scripts in parallel e.g. ./run_all.sh contains

./run1.sh &
./run2.sh &
./run3.sh &

wait
echo "All 3 complete"

How can I terminate all processes if one of them exits due to an error etc? The && requires each script to terminate successfully but it runs sequentially.

FedKad avatar
cn flag
Did you try the `wait -n` command? See: https://linuxize.com/post/bash-wait/
Score:0
in flag

You might try something like this:

  { run1.sh || killtheothers.sh 1 ; } &
  { run2.sh || killtheothers.sh 2 ; } &
  { run3.sh || killtheothers.sh 3 ; } &

so that each command is run in the background (via the &) but if any command fails (returns an error, i.e. non-zero, status) then the "or" control operator || allows the "killtheothers.sh" script to run. You'll have to write killtheothers.sh, perhaps using the "killall" command. I showed it using a parameter to specify which runX command the script wouldn't need to kill.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.