Challenge - 5 Problems
Background Process Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this script?
Consider this bash script that runs two background jobs and waits for them to finish before printing a message.
Bash Scripting
echo "Start" sleep 1 & sleep 2 & wait echo "Done"
Attempts:
2 left
💡 Hint
The wait command pauses the script until all background jobs finish.
✗ Incorrect
The script prints 'Start' immediately, then runs two sleep commands in the background. The wait command pauses until both sleeps finish, then prints 'Done'.
💻 Command Output
intermediate2:00remaining
What happens if you omit 'wait' in this script?
Given this script, what is the likely output?
Bash Scripting
echo "Begin" sleep 3 & echo "End"
Attempts:
2 left
💡 Hint
Without wait, the script does not pause for background jobs.
✗ Incorrect
The script prints 'Begin', starts sleep in background, then immediately prints 'End'. The sleep runs independently.
📝 Syntax
advanced2:00remaining
Which option correctly waits for a specific background job?
You start a background job with 'sleep 5 &' and want to wait only for that job. Which command waits for it correctly?
Bash Scripting
sleep 5 & JOB_PID=$! # Which wait command is correct?
Attempts:
2 left
💡 Hint
Use the PID stored in $! to wait for that job.
✗ Incorrect
wait $JOB_PID waits only for the job with that PID. wait alone waits for all background jobs. wait & is invalid syntax. wait sleep 5 is invalid.
🔧 Debug
advanced2:00remaining
Why does this script not wait as expected?
Script:
sleep 2 &
wait 12345
echo "Finished"
Assuming 12345 is not a valid PID, what happens?
Attempts:
2 left
💡 Hint
wait returns error if PID does not exist.
✗ Incorrect
wait with a non-existent PID returns immediately with an error, so the script continues and prints 'Finished' without waiting for the background job.
🚀 Application
expert3:00remaining
How to wait for multiple specific background jobs efficiently?
You start three background jobs:
sleep 3 &
PID1=$!
sleep 4 &
PID2=$!
sleep 5 &
PID3=$!
You want to wait for all three jobs to finish but only those specific ones. Which approach is best?
Attempts:
2 left
💡 Hint
wait can take multiple PIDs at once.
✗ Incorrect
Using 'wait' with all PIDs waits for all specified jobs concurrently. Using multiple wait commands sequentially is slower. wait alone waits for all background jobs, which may include others.