Sikau

Linux and the shell · Day 16 · about 50 min

Capstone: Health check and deploy

A strict-mode script with checks, logging, restarts and cron.

Keep the site alive

Write the script every small team ends up with: check that services are up, restart what's down, log what happened, warn when the disk is filling up — then schedule it.

Design the script

Write the steps in plain words before writing Bash. For each service: is it active? If not, restart it and log that you did. Then check disk usage on the root filesystem and log a warning above a threshold. Finally, make sure every line written to the log starts with a timestamp, so the team can read what happened overnight.

  • Strict mode at the top: set -euo pipefail.
  • A log function that prefixes the date and appends with tee -a.
  • A loop over nginx app using systemctl is-active --quiet.
  • Disk check: df --output=pcent / | tail -1 | tr -dc '0-9'.
  • Exit 0 when healthy, non-zero when something needed fixing.

Make the log path and the disk threshold variables at the top of the script so they're easy to change.

The rest of this lesson

  • The core loopexplain
  • Schedule and verifyexplain
  • The script's floworder
  • Read the cron linepredict
  • healthcheck.shchallenge
  • Badge quizquiz
  • Linux course complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 16 lessons of Linux and the shell