I want to run some commands in parallel. When all of these commands are finished start the next one. I though the following approach will work

#!/bin/bash
command1 &
command2 &
command3 &&
command4

but it didn't. I need to run command4 when all the first three commands have been completely finished.

share|improve this question
up vote 12 down vote accepted
#!/bin/bash
command1 &
command2 &
command3 &

wait
command4

wait (without any arguments) will wait until all the backgrounded processes have exited.

The complete description of wait in the bash manual:

wait [-n] [n ...]

Wait for each specified child process and return its termination status. Each n may be a process ID or a job specification; if a job spec 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 the -n option is supplied, wait waits for any job to terminate and returns its exit status. 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.