$_ CLI In Depth
Learning progress 0%
Build · Processes, Signals & Job Control

Build: A Process Supervisor That Restarts on Crash

Write supervise.sh to the spec, then run it with a failing command and check its log.

~30 min Intermediate 2 checked steps

Build it on a real Linux machine

The guided lab boots Linux in your browser. Each step runs a check script on that machine, so it passes only when your files and output are right.

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.

BASH14 lines
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
EOF

Step 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.

BASH1 lines
bash supervise.sh 2 false 2> supervise.log; echo "status $?" > run.txt

The 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.

Reference files

Read these after you have tried the lab. They are the solutions the checks were verified against.

supervise.sh

BASH19 lines
#!/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
done

walkthrough.sh

BASH30 lines
#!/usr/bin/env bash
# Reference solution for every step of the guided lab, in order.

# Step 1: Write supervise.sh to the spec
cd /workspace
cat > supervise.sh <<&#039;EOF&#039;
#!/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
done
EOF

# Step 2: Run a failing command and read the log
cd /workspace
bash supervise.sh 2 false 2> supervise.log; echo "status $?" > run.txt

Lessons this build uses