Bird
Raised Fist0
Bash Scriptingscripting~10 mins

wait for background processes in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - wait for background processes
Start script
↓
Launch background process
↓
Launch another background process
↓
Use 'wait' command
↓
Wait until all background processes finish
↓
Continue script execution
↓
End script
The script starts background tasks, then uses 'wait' to pause until all finish before continuing.
Execution Sample
Bash Scripting
sleep 3 &
sleep 2 &
wait
echo "All done"
Starts two background sleeps, waits for both to finish, then prints a message.
Execution Table
StepActionBackground Processes RunningOutput
1Start 'sleep 3' in backgroundsleep 3
2Start 'sleep 2' in backgroundsleep 3, sleep 2
3Execute 'wait' commandsleep 3, sleep 2
4Wait until 'sleep 2' finishes (after 2s)sleep 3
5Wait until 'sleep 3' finishes (after 3s)
6Print 'All done'All done
💡 'wait' ends when all background processes have finished
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Background Processesnonesleep 3sleep 3, sleep 2sleep 3, sleep 2sleep 3
Key Moments - 2 Insights
Why does the script pause at 'wait' even though the background processes started earlier?
'wait' pauses the script until all background processes finish, as shown in execution_table steps 3 to 5.
What happens if we remove 'wait' from the script?
The script would print 'All done' immediately without waiting, possibly before background tasks finish (not shown in table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what background processes are running after step 2?
Asleep 3 and sleep 2
Bsleep 3 only
Csleep 2 only
Dnone
💡 Hint
Check the 'Background Processes Running' column at step 2 in the execution_table.
At which step does the 'wait' command finish waiting for all background processes?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at when the background processes list becomes empty in the execution_table.
If the 'sleep 2' command took longer, how would that affect the execution table?
ANo change in timing
B'wait' would finish earlier
CStep 4 would be delayed, 'wait' would take longer
DThe script would skip 'wait'
💡 Hint
Consider how 'wait' depends on background processes finishing, as shown in steps 4 and 5.
Concept Snapshot
wait for background processes in bash:
- Use '&' to start a process in background
- Use 'wait' to pause script until all background jobs finish
- Script resumes only after all background tasks complete
- Without 'wait', script continues immediately
- Useful to synchronize tasks
Full Transcript
This example shows how a bash script starts two background sleep commands. Each runs independently. The 'wait' command pauses the script until both background processes finish. The execution table traces each step: starting background jobs, waiting, and finally printing 'All done'. The variable tracker shows how background processes exist and then finish. Key moments clarify why 'wait' pauses and what happens if omitted. The quiz tests understanding of process states and timing. This helps beginners see how to control script flow with background tasks.

Practice

(1/5)
1. What does the wait command do in a bash script when used without any arguments?
easy
A. It pauses the script until all background processes finish.
B. It immediately terminates all background processes.
C. It lists all running background processes.
D. It restarts the last background process.

Solution

  1. Step 1: Understand the role of wait without arguments

    The wait command without arguments pauses the script until all background jobs started by the script finish.
  2. Step 2: Compare with other options

    It does not terminate, list, or restart processes; it only waits for their completion.
  3. Final Answer:

    It pauses the script until all background processes finish. -> Option A
  4. Quick Check:

    wait without args = pause for all background jobs [OK]
Hint: No args means wait for all background jobs to finish [OK]
Common Mistakes:
  • Thinking wait kills processes
  • Assuming wait lists processes
  • Confusing wait with restart commands
2. Which of the following is the correct syntax to wait for a specific background process with PID stored in variable pid?
easy
A. wait pid
B. wait $pid
C. wait --pid $pid
D. wait -p $pid

Solution

  1. Step 1: Recall the syntax for waiting on a specific PID

    The correct syntax is wait $pid, where $pid is the process ID variable.
  2. Step 2: Identify incorrect options

    Options with flags like -p or --pid are invalid in bash. Also, wait pid without $ won't expand the variable.
  3. Final Answer:

    wait $pid -> Option B
  4. Quick Check:

    Use wait with $pid to wait for that process [OK]
Hint: Use wait followed by $pid to wait for one process [OK]
Common Mistakes:
  • Using flags like -p or --pid which don't exist
  • Forgetting the $ to expand variable
  • Passing variable name without $
3. Consider this script snippet:
sleep 3 & pid1=$!
sleep 5 & pid2=$!
wait $pid1
echo "First done"
wait $pid2
echo "Second done"

What will be the order of printed lines?
medium
A. First done\nSecond done
B. Second done\nFirst done
C. First done\nFirst done
D. Second done\nSecond done

Solution

  1. Step 1: Understand background jobs and wait order

    Two sleep commands run in background: one sleeps 3 seconds, the other 5 seconds. wait $pid1 waits for the 3-second sleep to finish first.
  2. Step 2: Trace the echo outputs

    After the first wait, it prints "First done". Then it waits for the 5-second sleep and prints "Second done".
  3. Final Answer:

    First done Second done -> Option A
  4. Quick Check:

    Waits print in order of completion [OK]
Hint: Wait commands print in order of waited process completion [OK]
Common Mistakes:
  • Assuming both echo print immediately
  • Confusing order of wait calls
  • Ignoring background process durations
4. What is wrong with this script snippet?
sleep 4 &
wait pid
echo "Done"

Assuming pid is not defined.
medium
A. The script will wait for all background jobs anyway.
B. The script will wait for a process named 'pid'.
C. The script will throw an error because pid is undefined.
D. The script will ignore the wait command.

Solution

  1. Step 1: Check variable usage in wait command

    The script uses wait pid without a $ sign, so bash treats 'pid' as a literal string, not a variable.
  2. Step 2: Understand effect of undefined variable

    Since 'pid' is not a valid PID and not defined, wait will fail with an error about invalid PID or job.
  3. Final Answer:

    The script will throw an error because pid is undefined. -> Option C
  4. Quick Check:

    Use $pid to expand variable; missing $ causes error [OK]
Hint: Always use $ before variable names in wait [OK]
Common Mistakes:
  • Forgetting $ before variable
  • Assuming wait accepts names
  • Ignoring error messages
5. You want to run three commands in background, capture their PIDs, and then wait specifically for those three processes to finish before continuing. Which script snippet correctly does this?
hard
A. cmd1 & pid1=$!; cmd2 & pid2=$!; cmd3 & pid3=$!; wait
B. cmd1 & cmd2 & cmd3 & wait $!
C. cmd1 & cmd2 & cmd3 & wait
D. cmd1 & pid1=$!; cmd2 & pid2=$!; cmd3 & pid3=$!; wait $pid1 $pid2 $pid3

Solution

  1. Step 1: Understand background process tracking

    Each command runs in background and its PID is saved in separate variables.
  2. Step 2: Use wait with all PIDs

    Using wait $pid1 $pid2 $pid3 waits for all three specific processes to finish.
  3. Step 3: Analyze other options

    cmd1 & cmd2 & cmd3 & wait $! (cmd1 & cmd2 & cmd3 & wait $!) waits only for last background job. cmd1 & cmd2 & cmd3 & wait (cmd1 & cmd2 & cmd3 & wait) waits for all background jobs but does not track PIDs explicitly. cmd1 & pid1=$!; cmd2 & pid2=$!; cmd3 & pid3=$!; wait saves PIDs but then uses wait without arguments, so the PID variables are unused and it waits for all jobs.
  4. Final Answer:

    cmd1 & pid1=$!; cmd2 & pid2=$!; cmd3 & pid3=$!; wait $pid1 $pid2 $pid3 -> Option D
  5. Quick Check:

    Wait with all PIDs ensures all specific jobs finish [OK]
Hint: Save PIDs and wait for all to ensure completion [OK]
Common Mistakes:
  • Waiting only for last PID
  • Not saving PIDs before wait
  • Assuming wait without args always works as expected
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard