What you will build
Step 1: Write supervise.sh to the spec
Create supervise.sh in /workspace. Its first argument is the maximum number of restarts and the rest is the command. Print each restart as a line on standard error. Exit with the status of the command.
The reference solution from the unit is the model. Write it with a heredoc, then run it once with a command that succeeds.
cat > supervise.sh <<'EOF'
#!/bin/bash
max=$1; shift
restarts=0
while true; do
"$@"
status=$?
if [ "$status" -eq 0 ]; then exit 0; fi
if [ "$restarts" -ge "$max" ]; then exit "$status"; fi
restarts=$((restarts + 1))
echo "restart $restarts after status $status" >&2
sleep 0.1
done
EOFStep 2: Run a failing command and read the log
Run supervise.sh with a maximum of two restarts and the command false. Send its standard error to supervise.log, and write the final status in run.txt in the form status N.
bash supervise.sh 2 false 2> supervise.log; echo "status $?" > run.txtThe log should show two restart lines and one giving-up line. The status is 1.
Files
supervise.sh: the finished script.walkthrough.sh: every step of the reference solution, in order.
How it is checked
Each step in the guided lab has a check script that runs on the lab machine after you finish. It passes only when the files and output it expects are there, so the lab cannot be completed by reading alone.