0
0
Bash Scriptingscripting~30 mins

Parallel execution patterns in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Parallel execution patterns
📖 Scenario: You are managing a server where you need to run multiple tasks at the same time to save time. Running tasks one after another takes too long. You want to learn how to run commands in parallel using Bash scripting.
🎯 Goal: Build a Bash script that runs three simple commands in parallel and waits for all of them to finish before showing a message.
📋 What You'll Learn
Create three simple commands that each sleep for a few seconds
Run these commands in parallel using background execution
Use a variable to count how many commands are running
Wait for all commands to finish before printing a completion message
💡 Why This Matters
🌍 Real World
Running multiple tasks at the same time saves time on servers and automation scripts. For example, downloading files or processing data in parallel.
💼 Career
System administrators and DevOps engineers often use parallel execution in Bash scripts to improve efficiency and manage multiple processes.
Progress0 / 4 steps
1
Create three simple commands
Create three variables called cmd1, cmd2, and cmd3. Set cmd1 to sleep 3, cmd2 to sleep 4, and cmd3 to sleep 2.
Bash Scripting
Need a hint?

Use simple assignment like cmd1="sleep 3" to store commands as strings.

2
Add a counter for running commands
Create a variable called running and set it to 0. This will count how many commands are running in parallel.
Bash Scripting
Need a hint?

Initialize the counter with running=0.

3
Run commands in parallel and update counter
Use $cmd1 &, $cmd2 &, and $cmd3 & to run the commands in the background. After each command, increase running by 1 using running=$((running + 1)).
Bash Scripting
Need a hint?

Run each command with & at the end to run in background. Increase running after each.

4
Wait for all commands and print completion message
Use the wait command to wait for all background commands to finish. Then print echo "All $running commands finished." to show the message.
Bash Scripting
Need a hint?

Use wait to pause until all background jobs finish, then print the message.