Skip to main content

Assignment 1: A system health-check script

Goal: Write a Bash script that inspects the machine it runs on — disk usage, free memory, and whether a chosen service is running — and prints a clear, readable report with a meaningful exit code.

Where: On the Jumpbox (10.100.100.254, Ubuntu, user ubuntu). Create your script in your home directory, e.g. ~/healthcheck.sh.

Tasks

  1. Create ~/healthcheck.sh starting with the shebang #!/usr/bin/env bash and the safety header set -euo pipefail. Add a top comment describing what the script does and its usage.
  2. Add a log() helper function that prints messages prefixed with a timestamp (use date +%T).
  3. Disk check: Get the percentage used of the root filesystem / (hint: df --output=pcent / | tail -1 | tr -dc '0-9'). Store it in a variable. Print Disk /: NN%. If usage is >= 90, label it CRITICAL; if >= 75, label it WARNING; otherwise OK. Use if/elif/else with numeric tests.
  4. Memory check: Report available memory. Use free -m and extract the "available" column, or print the human-readable line with free -h | grep Mem. Print Memory available: NN MB (or the line). Optionally flag WARNING if available memory is below a threshold you choose.
  5. Service check: Accept a service name as the first argument $1 (default to ssh if none is given). Use systemctl is-active --quiet "$svc" inside an if to report Service <name>: up or Service <name>: DOWN.
  6. Exit code: Track whether anything was CRITICAL or the service was down. Exit 0 if all healthy, non-zero (e.g. 1) if any problem was found, so another script could rely on it.
  7. Make the script executable (chmod +x ~/healthcheck.sh) and run it both with no argument and with a service name, e.g. ./healthcheck.sh cron.
  8. Run shellcheck ~/healthcheck.sh and fix every warning until it is clean.

Deliverable

The file ~/healthcheck.sh on the Jumpbox: executable, ShellCheck-clean, with a header comment, a log() function, the three checks, and a meaningful exit code. Paste a sample run (with no argument and with one argument) into your notes to show your mentor.

Acceptance criteria — you're done when:

  • The script begins with #!/usr/bin/env bash and set -euo pipefail.
  • ./healthcheck.sh runs without errors and prints a disk line, a memory line, and a service line.
  • Disk usage is labelled OK / WARNING / CRITICAL using numeric comparisons.
  • The service name comes from $1 and defaults sensibly when no argument is given.
  • The service status correctly shows up for a running service and DOWN for a stopped/unknown one (test with a real and a fake name).
  • Running echo $? after a healthy run prints 0, and after a problem (e.g. a fake service) prints non-zero.
  • shellcheck ~/healthcheck.sh reports no warnings.
  • Every variable expansion is double-quoted.

Hints

  • Default an argument: svc="${1:-ssh}" means "use $1, or ssh if $1 is unset."
  • To track problems, start problems=0 and do problems=$((problems + 1)) whenever a check fails; exit with 0 if problems is 0, else 1.
  • systemctl is-active --quiet name prints nothing and just sets an exit code — perfect for an if.
  • Test the failure path with a name that does not exist, e.g. ./healthcheck.sh notaservice.
  • Re-read Lesson 3 (conditionals) for if/elif and numeric tests, and Lesson 5 for the safety header and ShellCheck.
  • Keep functions small and quote everything — your future self reading this in Git will thank you.

Blocked for more than ~30 minutes after re-reading the lessons? Bring what you've tried to your mentor.