I know jhwilliams said "something like this" but I just wanted to point out that his example, as written, would be an infinite loop. Specifically:
would never evaluate to
false once the loop is entered. In other words, there is nothing
inside the loop to change the value of the pid_list variable.
I would also vote for
wait. Though, perhaps a merger of the efforts of jhwilliams and GamezR2EZ:
Code:
#!/bin/bash
# < Setup code is here>
# A variable containing a space-separated list of all process IDs spawned
pid_list=''
# Process #1
gnome-terminal -t "$BUILD_NAME: App #1" -e "$APP1 $BUILD_NAME $HOST_NAME $RUN_DIR" &
pid_list="$pid_list $!"
sleep 1
# Process #2
COMMAND="mpiexec -machinefile ../cfg/machine-file.txt -n 4 $APP2"
gnome-terminal -t "$BUILD_NAME: App #2" -e "$COMMAND" &
pid_list="$pid_list $!"
sleep 1
# Process #3
COMMAND="$APP3 -display $(hostname):0"
gnome-terminal -t "$BUILD_NAME: App #3" -e "$COMMAND" &
pid_list="$pid_list $!"
sleep 1
# Process #4
COMMAND="$APP4 -display $(hostname):0"
gnome-terminal -t "$BUILD_NAME: App #4" -e "$COMMAND" &
pid_list="$pid_list $!"
sleep 1
for processID in ${pid_list} ; do
wait ${processID}
done
# < Cleanup code is here>
NOTE: The pid_list assignment statements
must come before the sleep command. Otherwise, you'll get the process ID of the sleep command and not the gnome-terminal.
It waits for each process ID in sequence. There is a danger in this approach. Say process #1 runs for
days, but process 4 runs for a few seconds. There is a chance that process 4's process ID will be re-used by the system. So, it's possible that the script would end up waiting on a process it did not spawn itself.
To avoid that, you probably need to use a polling loop (like in jhwilliams original post) that checks each process ID in the list once every X seconds/minutes/hours. Then, if the process is gone, remove that ID from your checklist.
EDIT:
Corrected a few things... I referenced "waitpid" instead of "wait" which is incorrect. "waitpid" is a function in C... not a shell command.