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?
You can use
waita built-in command available in Bash and in some other shells.(see equivalent command WAITFOR on Windows)
waitdocumentationSimple solution
Below
waitwaits indefinitely for all currently active child processes to be all ended (i.e. in this case the three scripts).Store the PIDs in shell local variables
Store the PIDs in temporary files
This last solution pollutes the current directory with three files (
1.pid,2.pidand3.pid). One of these file may be corrupted beforewaitcall. Moreover these files could be left in the file-system in case of crash.