AI Hints
Home›Linux Tutorial›More Projects
Linux · Lesson 112 of 120

Project: Health Check

Is everything OK?.

⏱ 2 min read▶ Video at 2:29:59🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Admins like a quick answer to one question. Is this computer OK? Let's build a health check script that shows disk, memory, and processes, and warns us when the disk is getting full.

Project: Health Check
From the video: Project: Health Check
  • Is this computer OK?
  • Disk, memory, processes
  • Warn when the disk is full

health.sh

Dollar, brace, one, colon dash ninety means, use the first argument, or ninety if there isn't one. DF with dash dash output gives just the percentage used, and TR strips everything but the digits. Awk picks the available memory, and PS counts the processes. Finally, if compares the disk with the limit.

nano health.shbash
#!/bin/bash
limit="${1:-90}"
disk=$(df --output=pcent / | tail -n 1 | tr -dc 0-9)
echo "== Health check: $(hostname), $(date +%F) =="
echo "Disk used:  $disk%"
echo "Memory free: $(free -h | awk '/Mem/ {print $7}')"
echo "Processes:  $(ps -e --no-headers | wc -l)"
if [ "$disk" -gt "$limit" ]; then
  echo "WARNING: disk is over $limit%!"
else
  echo "Disk OK (limit $limit%)"
fi

Checking the health

With the normal limit, everything's fine. Disk OK. Now we pass a tiny limit, ten percent, to test the warning. This time the script warns us. Always test the unhappy path too.

Terminal
./health.sh
== Health check: laptop, 2026-01-05 ==
Disk used:  41%
Memory free: 8.6Gi
Processes:  5
Disk OK (limit 90%)
./health.sh 10
== Health check: laptop, 2026-01-05 ==
Disk used:  41%
Memory free: 8.6Gi
Processes:  5
WARNING: disk is over 10%!

Where to take it

Put the health check in cron, every hour, and save its output to a log. Or only send a message when there's a warning. The same idea scales up to the monitoring tools big companies use. They're just much fancier health checks.

keyvalue
${1:-90}argument, or a default
df --output=pcentjust the percentage
tr -dc 0-9keep only digits
cron hourlycheck all the time

Summary

The health check reports disk, memory, and processes, and warns when the disk passes a limit. Next, a backup script that tidies up after itself.

Key takeaways
  • ${1:-default}
  • df, free, ps
  • if [ "$disk" -gt "$limit" ]
Next up: Project: Nightly Backup — with old backups removed