(Sorry, but my original answer was pretty wrong ... Here is the fix)
Using $? to get the exit status of the background process in startProcess.sh leads to an incorrect result. Man bash :
Special Parameters ? Expands to the status of the most recently executed foreground pipeline.
As you mentioned in your comment, the correct way to get the exit status of a background process is to use the built-in wait . But for this, bash must handle the SIGCHLD signal.
I made a small test environment to show how it can work:
Here is the script loop.sh to run in the background:
#!/bin/bash [ "$1" == -x ] && exit 1; cnt=${1:-500} while ((++c<=cnt)); do echo "SLEEPING [$$]: $c/$cnt"; sleep 5; done
If arg is -x , then it exits with an exit status of 1 to simulate an error. If arg is num, then it waits num * 5 seconds to print SLEEPING [<PID>] <counter>/<max_counter> to stdout.
The second is the script launcher. It runs 3 loop.sh scripts in the background and prints their exit status:
#!/bin/bash handle_chld() { local tmp=() for i in ${!pids[@]}; do if [ ! -d /proc/${pids[i]} ]; then wait ${pids[i]} echo "Stopped ${pids[i]}; exit code: $?" unset pids[i] fi done } set -o monitor trap "handle_chld" CHLD
The handle_chld function will process SIGCHLD signals. The monitor setting allows a non-interactive script to receive SIGCHLD. Then a trap is set for the SIGCHLD signal.
Then the background processes are started. All their PIDs are stored in the pids array. If SIGCHLD is received, it is checked among / proc / directories whose child process has been stopped (missing) (it could also be checked using kill -0 <PID> bash built-in). After waiting, the exit status of the background process is stored in the well-known pseudo-variable $? .
The main script waits for all pids to stop (otherwise it would not be able to get the exit status of its children), and it stops.
Output Example:
WAITING FOR: 13102 13103 13104 SLEEPING [13103]: 1/2 SLEEPING [13102]: 1/3 Stopped 13104; exit code: 1 WAITING FOR: 13102 13103 WAITING FOR: 13102 13103 SLEEPING [13103]: 2/2 SLEEPING [13102]: 2/3 WAITING FOR: 13102 13103 WAITING FOR: 13102 13103 SLEEPING [13102]: 3/3 Stopped 13103; exit code: 0 WAITING FOR: 13102 WAITING FOR: 13102 WAITING FOR: 13102 Stopped 13102; exit code: 0 STOPPED
You can see that the exit codes are reported correctly.
Hope this helps a bit!