The specification
Write a script named supervise.sh in /workspace. It takes a maximum restart count as its first argument and the command to run as the remaining arguments. It runs the command in the foreground. If the command exits with status 0, the supervisor prints nothing extra, exits 0, and stops. If the command fails, the supervisor prints a line to standard error that says which restart it is about to make, waits a short time, and runs the command again. After the maximum number of restarts, it prints a final message and exits with the last status of the command.
The script must keep the exit status of the child, not the status of the echo or the sleep. It must not restart a command that succeeded. It must refuse to run without a command. These rules are small, but they are the place where most first attempts go wrong.
Design decisions
The supervisor runs the child directly, not through a string. Using the arguments as an array of words avoids quoting problems, and the child receives exactly the arguments you passed. The script saves the status right after the command runs, because every later command overwrites $?. The restart count lives in a plain variable, which the loop compares to the maximum before each new attempt.
A short pause between restarts keeps a crashing program from using all the CPU. The pause in this specification is a tenth of a second, which is short enough for a lab and long enough for a real program to show up as a repeated failure. A production supervisor would use an exponential backoff, which doubles the pause after each failure.
What the solution must demonstrate
A correct supervisor passes three simple tests. First, a command that succeeds on the first try runs once, and the supervisor exits 0. Second, a command that always fails is run exactly one more time than the maximum allows, and the supervisor exits with that command status. Third, the messages on standard error show the restart numbers in order, which lets you check the count without reading the code.
To test the second rule, use the command false, which always exits with status 1. With a maximum of two restarts, the program runs three times in total: once at the start and twice more. The supervisor then exits 1. The three attempts are visible as two restart messages followed by the final message.
Reference solution
The reference below is one way to satisfy the specification. It uses a function-free loop, a saved status, and a counter. Read it line by line, and compare each line with the rule it implements.
#!/bin/bash
# usage: supervise.sh MAX_RESTARTS COMMAND [ARGS...]
max=$1; shift
if [ "$#" -eq 0 ]; then echo "no command" >&2; exit 2; fi
restarts=0
while true; do
"$@"
status=$?
if [ "$status" -eq 0 ]; then
exit 0
fi
if [ "$restarts" -ge "$max" ]; then
echo "giving up after $restarts restarts, status $status" >&2
exit "$status"
fi
restarts=$((restarts + 1))
echo "restart $restarts after status $status" >&2
sleep 0.1
doneThe first line after the shebang shifts the maximum off the argument list, so "$@" holds only the command. The check for an empty command runs before the loop. Inside the loop the command runs, its status is saved, and a zero status exits at once. The restart branch compares the counter to the maximum, and it either gives up with the last status or increments the counter and pauses.
Write the script before you test it. Then run it once with a command that succeeds, once with a command that fails, and once with no command. Each run shows a different branch of the code.
Extensions to try
Once the basic version works, you can extend it. Record each restart in a log file that is passed as an environment variable. Stop restarting when the child runs for longer than a set time, which tells the supervisor the failure was transient. Forward SIGTERM to the child so that stopping the supervisor also stops the program. Each change adds one rule, so test each one before you add the next.
Hands-on: write and run the supervisor
In the lab, create the script in /workspace, make it executable, and run it with a command that always fails. The messages go to standard error, which you can capture in a file for the check.
bash supervise.sh 2 false 2> supervise.log; echo "status $?"The log should contain two restart messages and one giving-up message. The status printed at the end is 1, because false exits with 1. Run the same script with true and check that the status is 0 with no restart messages at all.