$_ CLI In Depth
Learning progress 0%
build performance tmux vim git

Build: A System Health Report Script

5 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:03

The plan

The report has three sections. The memory section comes from free, the disk section from df, and the load section from the load file in /proc. Each section starts with a heading line, so the output can be read by a person and searched by a script. The tool takes an optional output file, and without one it prints to standard output. Each step in the lab adds one property to this basic design.

Sources and headings

free -m prints the memory table in megabytes. df -h /workspace prints the disk usage of the file system that holds the workspace, in human-readable units. cat /proc/loadavg prints the three load averages and the running process counts. A heading is one echo line before each source, written in a fixed pattern such as two equals signs, the title, and two equals signs. A fixed pattern lets a later script split the report by section without guessing.

BASH2 lines
echo "== Memory =="
free -m

The report should be read by people first. Keep the headings short, and use the tools default output rather than reformatting it, so that anyone who knows the command recognizes the numbers.

Strict mode and failures

A report that silently skips a section is worse than no report, because it looks complete. Strict mode makes a missing command or an unset variable stop the script with a message. set -euo pipefail after the shebang gives that behavior, and pipefail matters because a report that pipes its output through a filter should fail when the source fails. The script should exit with a non-zero status when any section fails, so that a scheduler can notice.

Note

With set -e, a command whose failure you expect must be written so that the failure does not stop the script, for example with an || true at the end of the line. Use that form sparingly, and only when the failure is harmless.

An output option

A tool that always prints to standard output is hard to schedule, because a scheduler wants a file. An option such as -o FILE solves that while keeping the default. The script checks its first argument, and if it is -o, it sends the output to the next argument. Otherwise it keeps standard output. A brace group around the three sections means one redirection covers them all.

BASH6 lines
out=/dev/stdout
if [ "${1:-}" = "-o" ]; then out="$2"; fi
{
  echo "== Disk =="
  df -h /workspace
} > "$out"

The default value in ${1:-} avoids an unset-variable error when no argument is given, which matters under set -u.

Running it on a schedule

A scheduler such as cron runs a command with a minimal environment, so the script should set its own paths and never depend on the current directory. Write the report to a file with a name that includes the date, and keep the most recent few. Those habits make the tool reliable when nobody is watching it.

Checking the result

Run the report by hand, then open the file and read each section. A report that prints the right headings but the wrong numbers is still wrong, so compare one value with the tool that produced it. Finally, check the exit status with echo of the status after the run, which confirms that the failure path works as well as the success path.

Save the script in a bin directory, give it a usage line, and keep it under version control. The health report is small, but it is the same kind of tool as the ones you will write for real problems.

Failure paths

A report should say what it could not read. If df fails because the path does not exist, the script should print a line that names the failed section and then exit with a non-zero status, rather than skipping the heading. A reader then knows the section is missing on purpose. Writing that line costs a few lines of script, and it saves a wrong conclusion later.

Keeping old reports

A scheduled report that always writes to the same file overwrites the history. Name each file with the date and a short tag, and delete the oldest files with a simple loop once a limit is reached. The trend across the saved files is often more useful than any single report, because it shows growth that a single snapshot cannot.

Keeping the output readable

A report is read on a small screen or in a log viewer, so keep each line short and avoid wide tables when a list would do. Use the same unit for each number, and print the time of the report at the top so that a reader knows how old the numbers are. When a section has many lines, cut it to the first few and say so in the heading.

Terminal lab · 3 steps · real Linux in your browser

Build a system health report script

Write a health report script with sections, strict mode, and an output file option.

01
Write the report with three sections
Write health.sh. It prints a heading and the output of one command for memory, disk, and load: ``bash cat > health.sh <<'EOF' #!/bin/bash echo "== Memory ==" free -m echo "== Disk ==" df -h /workspace echo "== Load ==" cat /proc/loadavg EOF bash health.sh > report.txt `` Open report.txt. It should have the headings Memory, Disk, and Load.
Show hint for step 1
Each heading is an echo before a command. Redirect the whole script to report.txt when you run it.
02
Turn on strict mode
Strict mode stops a script early when something fails. Add set -euo pipefail on the line after the shebang: ``bash sed -i '1a set -euo pipefail' health.sh bash health.sh > report.txt echo "exit status: $?" `` The report should still show all three headings, and the script should finish with status 0.
Show hint for step 2
sed 1a inserts text after line 1. -e stops on errors, -u rejects unset variables, and pipefail reports a failed command in a pipeline.
03
Write to a file with -o
Let the user choose the output file. Rewrite health.sh so that -o FILE sends the report to FILE, and with no option it prints to standard output: ``bash #!/bin/bash set -euo pipefail out=/dev/stdout if [ "${1:-}" = "-o" ]; then out="$2"; fi { echo "== Memory ==" free -m echo "== Disk ==" df -h /workspace echo "== Load ==" cat /proc/loadavg } > "$out" `` Run bash health.sh -o summary.txt and open summary.txt.
Show hint for step 3
The braces group the three sections so that one redirection covers them all. The default value in ${1:-} avoids an error when no argument is given.
Check yourself Question 1 of 4
Concept Check Report sources recall

Which three sources does the health report combine?

Recap & Key Takeaways

A health report combines free -m, df -h, and /proc/loadavg under fixed headings that a script can split on.
set -euo pipefail makes a report fail loudly instead of printing a partial picture that looks complete.
An -o FILE option keeps standard output as the default while letting a scheduler collect a file.
Check the exit status and one value against the source tool before trusting a report.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue