Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Barrier in bash, can it be done easily?

Tags:

bash

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?

like image 691
Christian Neverdal Avatar asked Feb 04 '12 23:02

Christian Neverdal


People also ask

Is bash space sensitive?

Bash (especially sh) is case- and space-sensitive.

Does bash wait for previous command to finish?

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.

Does bash exit if command fails?

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.

How do you clear a string in bash?

Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty"

How do I stop bash from running?

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.


1 Answers

You can use wait a built-in command available in Bash and in some other shells.
(see equivalent command WAITFOR on Windows)

wait documentation

Wait 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 is 127. Otherwise, the return status is the exit status of the last process or job waited for.

Simple solution

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

Store the PIDs in shell local variables

./script1 & pid1=$!
./script2 & pid2=$!
./script3 & pid3=$!
wait $pid1 $pid2 $pid3  # waits for 3 PIDs
./script4

Store the PIDs in temporary files

./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.

like image 54
oHo Avatar answered Sep 28 '22 15:09

oHo