pbjiaq
Last Updated: December 26, 2018
·
19.33K
· gaqzi

Waiting for all background jobs to finish in bash

I have a script that collects some information from several different
sources, commands that should run in parallel. As soon as all
this data has been collected I want to process it but it's important
that I don't start this until all commands has finished.

With the wait command in bash you can force the execution of
the script to pause until a specific or all background jobs has
finished executing before continuing the execution of your script.

So by putting my collection jobs in the background and putting in a
wait I manged to speed up the total execution time by quite a lot.

Example script:

#!/bin/bash
collect-job1.sh &
collect-job2.sh &
collect-job3.sh &

wait

process-job-output.sh
wait # to not exit the containing script until all execution has finished

1 Response
Add your response

12656

Could you expand this tip to include nohup processes ? I'm assuming that if you close the terminal while your processes are running in the background, they will be killed.

over 1 year ago ·