Let's say I have a bash script that executes three scripts in parallel
./script1 &
./script2 &
./script3 &
Now, let us say that ./script4 depends on script1, script2 and script3. How can I force it to wait for those, while still executing the three scripts in parallel?
Bash (especially sh) is case- and space-sensitive.
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
Exit When Any Command FailsThis can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.
Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty"
If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.
You can use wait
a built-in command available in Bash and in some other shells.
(see equivalent command WAITFOR on Windows)
wait
documentationWait for each specified process to complete and return its termination status.
Syntax wait [n ...] Key n A process ID or a job specification
Each
n
can be a process ID or a job specification; if a job specification is given, all processes in that job's pipeline are waited for.If
n
is not given, all currently active child processes are waited for, and the return status is zero.If
n
specifies a non-existent process or job, the return status is127
. Otherwise, the return status is the exit status of the last process or job waited for.
Below wait
waits indefinitely for all currently active child processes to be all ended (i.e. in this case the three scripts).
./script1 &
./script2 &
./script3 &
wait # waits for all child processes
./script4
./script1 & pid1=$!
./script2 & pid2=$!
./script3 & pid3=$!
wait $pid1 $pid2 $pid3 # waits for 3 PIDs
./script4
./script1 & echo $! >1.pid
./script2 & echo $! >2.pid
./script3 & echo $! >3.pid
wait $(<1.pid) $(<2.pid) $(<3.pid)
rm 1.pid 2.pid 3.pid # clean up
./script4
This last solution pollutes the current directory with three files (1.pid
, 2.pid
and 3.pid
). One of these file may be corrupted before wait
call. Moreover these files could be left in the file-system in case of crash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With