0
0
Bash Scriptingscripting~5 mins

Parallel execution patterns in Bash Scripting

Choose your learning style9 modes available
Introduction
Running tasks at the same time saves time and makes your scripts faster.
You want to download multiple files at once.
You need to run several commands that do not depend on each other.
You want to speed up processing by using multiple CPU cores.
You want to start background jobs and continue working without waiting.
You want to run tests or scripts in parallel to save time.
Syntax
Bash Scripting
command1 &
command2 &
wait
The ampersand (&) runs the command in the background.
The wait command pauses the script until all background jobs finish.
Examples
Runs two sleep commands at the same time, then waits for both to finish before printing.
Bash Scripting
sleep 3 &
sleep 5 &
wait
echo "All done"
Downloads two files in parallel, then waits for both downloads to complete.
Bash Scripting
curl http://example.com/file1 &
curl http://example.com/file2 &
wait
Starts three tasks in parallel with random sleep times, then waits for all to finish.
Bash Scripting
for i in {1..3}; do
  echo "Task $i started"
  sleep $((RANDOM % 3 + 1)) &
done
wait
echo "All tasks finished"
Sample Program
This script starts three sleep commands at the same time. It waits until all finish, then prints a message.
Bash Scripting
#!/bin/bash

echo "Starting tasks..."

sleep 2 &
sleep 4 &
sleep 3 &

wait
echo "All tasks completed!"
OutputSuccess
Important Notes
Use & to run commands in the background so your script can do other things.
Use wait to pause until all background tasks finish before moving on.
Be careful with too many parallel jobs; it can slow your system down.
Summary
Parallel execution runs multiple commands at the same time.
Use & to start background jobs and wait to pause until they finish.
This helps save time when tasks do not depend on each other.