Linux – How to run two different long running commands at the same time in a bash script

bashlinuxscripting

I have to chmod and chown hundreds of thousands of files as part of a migration script. Each command takes about an hour and a half to complete. I realized these two operations can be run at the same time which cuts down on running time, which I confirmed by testing in the shell.

I know the trick of pushing commands into the background with '&', but I need to make sure both processes finish before proceeding with the rest of the script.

Thanks

Best Answer

Use the wait command.

This demo:

#!/bin/bash
echo $SECONDS
sleep 12&
sleep 15&
jobs
wait
echo $SECONDS
echo "jobs are done"

Produces this output:

0
[1]-  Running                 sleep 12 &
[2]+  Running                 sleep 15 &
15
jobs are done

There's a fifteen second pause before the last two lines are output.