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
- Create
~/healthcheck.shstarting with the shebang#!/usr/bin/env bashand the safety headerset -euo pipefail. Add a top comment describing what the script does and its usage. - Add a
log()helper function that prints messages prefixed with a timestamp (usedate +%T). - 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. PrintDisk /: NN%. If usage is>= 90, label itCRITICAL; if>= 75, label itWARNING; otherwiseOK. Useif/elif/elsewith numeric tests. - Memory check: Report available memory. Use
free -mand extract the "available" column, or print the human-readable line withfree -h | grep Mem. PrintMemory available: NN MB(or the line). Optionally flagWARNINGif available memory is below a threshold you choose. - Service check: Accept a service name as the first argument
$1(default tosshif none is given). Usesystemctl is-active --quiet "$svc"inside anifto reportService <name>: uporService <name>: DOWN. - Exit code: Track whether anything was CRITICAL or the service was down. Exit
0if all healthy, non-zero (e.g.1) if any problem was found, so another script could rely on it. - 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. - Run
shellcheck ~/healthcheck.shand 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 bashandset -euo pipefail. -
./healthcheck.shruns without errors and prints a disk line, a memory line, and a service line. - Disk usage is labelled
OK/WARNING/CRITICALusing numeric comparisons. - The service name comes from
$1and defaults sensibly when no argument is given. - The service status correctly shows
upfor a running service andDOWNfor a stopped/unknown one (test with a real and a fake name). - Running
echo $?after a healthy run prints0, and after a problem (e.g. a fake service) prints non-zero. -
shellcheck ~/healthcheck.shreports no warnings. - Every variable expansion is double-quoted.
Hints
- Default an argument:
svc="${1:-ssh}"means "use$1, orsshif$1is unset." - To track problems, start
problems=0and doproblems=$((problems + 1))whenever a check fails;exitwith0ifproblemsis0, else1. systemctl is-active --quiet nameprints nothing and just sets an exit code — perfect for anif.- Test the failure path with a name that does not exist, e.g.
./healthcheck.sh notaservice. - Re-read Lesson 3 (conditionals) for
if/elifand 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.
No comments to display
No comments to display