Create a CLI Tool to Monitor System Health Using Bash

Create a CLI Tool to Monitor System Health Using Bash

Create a CLI Tool to Monitor System Health Using Bash

 

Monitoring system health doesn’t always require elaborate dashboards or third-party tools. With a few lines of Bash, you can build a powerful command-line interface (CLI) that keeps a close watch on CPU load, memory usage, disk space, and more. In this blog post, we’ll walk through building a lightweight Bash script that performs system health checks and sends alerts when thresholds are exceeded. You’ll also learn how to schedule it using cron for regular monitoring.

1. Setting Up Your Bash Script

Let’s start by creating an executable Bash script file. Name it healthcheck.sh and make sure it has executable permissions.

touch healthcheck.sh
chmod +x healthcheck.sh

Begin your script with the shebang line:

#!/bin/bash

This ensures you use the bash interpreter regardless of the user’s shell.

2. Monitor CPU Usage

You can extract real-time CPU load by interpreting the uptime or top commands. We’ll use top for a clean one-liner.

CPU_LOAD=$(top -bn1 | grep "load average:" | awk '{print $10}' | sed 's/,//')
THRESHOLD=1.5

if (( $(echo "$CPU_LOAD > $THRESHOLD" | bc -l) )); then
  echo "[WARNING] High CPU Load: $CPU_LOAD" >&2
fi

This snippet retrieves the 1-minute load average and compares it to a threshold (1.5 in this case). If exceeded, it prints an alert.

Tip: Adjust the threshold based on the number of CPU cores on the system. A quad-core system can handle higher load.

3. Monitor Memory Usage

To check memory usage, we can use the free command. Here’s how to get and evaluate the percentage of used memory:

MEM_TOTAL=$(free -m | awk '/^Mem:/ {print $2}')
MEM_USED=$(free -m | awk '/^Mem:/ {print $3}')
MEM_PERCENT=$(echo "scale=2; $MEM_USED/$MEM_TOTAL * 100" | bc)

if (( $(echo "$MEM_PERCENT > 80.0" | bc -l) )); then
  echo "[WARNING] High Memory Usage: $MEM_PERCENT%" >&2
fi

This calculates the percentage of memory used and notifies if it exceeds 80%. You can tailor this limit for your environment.

4. Monitor Disk Space

Disk space issues can quickly become critical. Use df to check space and alert if usage rises above 90%:

DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')

if [ "$DISK_USAGE" -gt 90 ]; then
  echo "[WARNING] High Disk Usage: $DISK_USAGE%" >&2
fi

This example checks the root partition. If you have multiple mounts or volumes, loop over them using df -h and awk.

5. Assembling the Script and Automating with Cron

Now, let’s combine all parts into a single script:

#!/bin/bash

# CPU Check
CPU_LOAD=$(top -bn1 | grep "load average:" | awk '{print $10}' | sed 's/,//')
THRESHOLD=1.5
if (( $(echo "$CPU_LOAD > $THRESHOLD" | bc -l) )); then
  echo "[WARNING] High CPU Load: $CPU_LOAD" >&2
fi

# Memory Check
MEM_TOTAL=$(free -m | awk '/^Mem:/ {print $2}')
MEM_USED=$(free -m | awk '/^Mem:/ {print $3}')
MEM_PERCENT=$(echo "scale=2; $MEM_USED/$MEM_TOTAL * 100" | bc)
if (( $(echo "$MEM_PERCENT > 80.0" | bc -l) )); then
  echo "[WARNING] High Memory Usage: $MEM_PERCENT%" >&2
fi

# Disk Check
DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 90 ]; then
  echo "[WARNING] High Disk Usage: $DISK_USAGE%" >&2
fi

Scheduling with crontab:

To run this script every 5 minutes, edit the crontab with crontab -e and add:

*/5 * * * * /path/to/healthcheck.sh >> /var/log/health.log 2>&1

This keeps logs of the health reports while capturing warnings as standard error output.

Conclusion

With a short and efficient Bash CLI tool, you can monitor key system metrics like CPU, memory, and disk space—all without adding heavy dependencies. This script is easy to schedule, quick to customize, and ideal for small servers or containers where simplicity and scriptability win. For more advanced needs, consider extending it to send email alerts or integrate with messaging platforms like Slack or Discord using webhooks.

By placing this tool into automated workflows with cron, you’ll keep a reliable eye on your infrastructure, giving you peace of mind and early warnings before bottlenecks occur.

 

Useful links: