2

I have a bash scripts in which I invoke other scripts to run in parallel. With the wait command I can wait until all parallel processes have finished. But I want to know if all the processes that executed in background in parallel executed successfully (with return code 0).

My code looks like:

--calling multiple processes to execute in backgroud
process-1 &
process-2 &
process-3 &
wait
--after parallel execution finishes I want to know if all of them were successful and returned '0'
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
krackoder
  • 2,841
  • 7
  • 42
  • 51
  • 1
    Possible duplicate of [shell - get exit code of background process](http://stackoverflow.com/questions/1570262/shell-get-exit-code-of-background-process) – dfogni Mar 18 '16 at 20:17

3 Answers3

1

You can use wait -n which returns the exit code of the next job that terminates. Call it once for each background process.

process-1 &
process-2 &
process-3 &
wait -n && wait -n && wait -n
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

wait -n seems the correct solution, but since it is not present in bash 4.2.37 you can try this trick:

#!/bin/bash    
(
    process-1    ||  echo $! failed &
    process-2    ||  echo $! failed &
    process-2    ||  echo $! failed &
    wait
) | grep -q failed

if [ $? -eq 0 ]; then
    echo at least one process failed
else
    echo all processes finished successfully
fi

Just make sure the string "failed" is not returned by the processes themselves while having an actual success. Also you could run processes with stdin and stderr redirected do /dev/null with process-1 &>/dev/null

user2021201
  • 370
  • 3
  • 10
0

I've written a tool that simplifies the solutions a bit: https://github.com/wagoodman/bashful

You provide a file describing what you want to run...

# awesome.yaml

tasks:
  - name: My awesome tasks
    parallel-tasks:
      - cmd: ./some-script-1.sh
      - cmd: ./some-script-2.sh
      - cmd: ./some-script-3.sh
      - cmd: ./some-script-4.sh

...and run it like so:

bashful run awesome.yaml

Then it will run your tasks in parallel with a vertical progress bar showing the status of each task. Failures are indicated in red and the program exits with 1 if there were any errors found (exit occurs after the parallel block completes).

wagoodman
  • 143
  • 2
  • 7