0
0
Bash Scriptingscripting~20 mins

wait for background processes in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Background Process Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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"
AStart
BDone\nStart
CStart\nDone
DDone
Attempts:
2 left
💡 Hint
The wait command pauses the script until all background jobs finish.
💻 Command Output
intermediate
2: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"
AEnd\nBegin
BBegin\nEnd\n(wait for 3 seconds)
CBegin\n(wait 3 seconds)\nEnd
DBegin\nEnd
Attempts:
2 left
💡 Hint
Without wait, the script does not pause for background jobs.
📝 Syntax
advanced
2: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?
Await $JOB_PID
Bwait
Cwait &
Dwait sleep 5
Attempts:
2 left
💡 Hint
Use the PID stored in $! to wait for that job.
🔧 Debug
advanced
2: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?
Await returns immediately with an error; 'Finished' prints right away
BScript crashes with syntax error
Cwait ignores invalid PID and waits for all jobs
Dwait blocks forever; 'Finished' never prints
Attempts:
2 left
💡 Hint
wait returns error if PID does not exist.
🚀 Application
expert
3: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?
Await $PID1 && wait $PID2 && wait $PID3
Bwait $PID1 $PID2 $PID3
Cwait
Dwait $PID1 & wait $PID2 & wait $PID3
Attempts:
2 left
💡 Hint
wait can take multiple PIDs at once.