0
0
Bash Scriptingscripting~10 mins

wait for background processes in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to wait for all background processes to finish before continuing.

Bash Scripting
echo "Start"; sleep 2 & sleep 3 & [1]; echo "Done"
Drag options to blanks, or click blank then click option'
Aexit
Bsleep
Cwait
Dkill
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sleep' instead of 'wait' does not wait for background jobs.
Using 'kill' or 'exit' will stop the script or processes incorrectly.
2fill in blank
medium

Complete the code to wait for a specific background process using its process ID.

Bash Scripting
sleep 5 & pid=$!; echo "Waiting for process"; [1] $pid; echo "Process done"
Drag options to blanks, or click blank then click option'
Await
Bsleep
Ckill
Djobs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'kill' will terminate the process instead of waiting.
Using 'jobs' only lists jobs but does not wait.
3fill in blank
hard

Fix the error in the code to properly wait for all background jobs before printing "Finished".

Bash Scripting
sleep 4 & sleep 3 & sleep 2 & [1]; echo "Finished"
Drag options to blanks, or click blank then click option'
Await $!
Bwait
Csleep
Dkill
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'wait $!' waits only for the last job, not all jobs.
Using 'sleep' or 'kill' does not wait properly.
4fill in blank
hard

Fill both blanks to create a loop that waits for each background job individually using their PIDs stored in an array.

Bash Scripting
pids=(); sleep 3 & pids+=($!); sleep 2 & pids+=($!); for pid in "${pids[@]}"; do [1] $pid; done; echo [2]
Drag options to blanks, or click blank then click option'
Await
Bsleep
C"All jobs done"
D"Done"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sleep' inside the loop does not wait for jobs.
Printing the wrong message or before waiting.
5fill in blank
hard

Fill all three blanks to start three background jobs, wait for all, and then print a summary message.

Bash Scripting
sleep 1 & sleep 2 & sleep 3 & [1]; echo [2] [3]
Drag options to blanks, or click blank then click option'
Await
B"All"
C"background jobs finished"
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sleep' instead of 'wait' does not wait for jobs.
Printing message before waiting.