Bash scripting is often seen as the duct tape of system administration, but it can also be a surprisingly powerful tool for proactive monitoring and alerting.

Here’s a script that checks disk space, memory usage, and running processes, and sends an email alert if any thresholds are breached.

#!/bin/bash

# --- Configuration ---
DISK_THRESHOLD=90          # Percentage of disk space used
MEMORY_THRESHOLD=85        # Percentage of memory used
PROCESS_NAME="critical_service" # Name of the process to monitor
ALERT_EMAIL="admin@example.com"
HOSTNAME=$(hostname)

# --- Disk Space Check ---
echo "Checking disk space..."
df -h --output=source,pcent | while read -r line; do
    if [[ "$line" =~ ^/dev/ ]]; then
        percent_used=$(echo "$line" | awk '{print $2}' | sed 's/%//')
        mount_point=$(echo "$line" | awk '{print $1}')
        if [ "$percent_used" -ge "$DISK_THRESHOLD" ]; then
            alert_message="Disk usage on $HOSTNAME is high: $mount_point is at ${percent_used}% (Threshold: ${DISK_THRESHOLD}%)."
            echo "$alert_message"
            echo "$alert_message" | mail -s "ALERT: High Disk Usage on $HOSTNAME" "$ALERT_EMAIL"
        fi
    fi
done

# --- Memory Usage Check ---
echo "Checking memory usage..."
mem_info=$(free | grep Mem:)
total_mem=$(echo "$mem_info" | awk '{print $2}')
used_mem=$(echo "$mem_info" | awk '{print $3}')
percent_used_mem=$(( used_mem * 100 / total_mem ))

if [ "$percent_used_mem" -ge "$MEMORY_THRESHOLD" ]; then
    alert_message="Memory usage on $HOSTNAME is high: ${percent_used_mem}% (Threshold: ${MEMORY_THRESHOLD}%)."
    echo "$alert_message"
    echo "$alert_message" | mail -s "ALERT: High Memory Usage on $HOSTNAME" "$ALERT_EMAIL"
fi

# --- Process Check ---
echo "Checking process '$PROCESS_NAME'..."
if pgrep -x "$PROCESS_NAME" > /dev/null
then
    echo "Process '$PROCESS_NAME' is running."
else
    alert_message="Process '$PROCESS_NAME' is not running on $HOSTNAME."
    echo "$alert_message"
    echo "$alert_message" | mail -s "ALERT: Process '$PROCESS_NAME' Down on $HOSTNAME" "$ALERT_EMAIL"
fi

echo "Monitoring script finished."

This script works by first defining configurable thresholds for disk space, memory, and the name of a critical process. It then uses standard Linux commands like df, free, and pgrep to gather system metrics. If any of these metrics exceed their defined thresholds, an email alert is sent to the specified address.

The df -h --output=source,pcent command provides a human-readable output of disk usage, showing the percentage used for each mounted filesystem. The awk and sed commands then parse this output to extract the percentage and mount point. The free command gives a breakdown of memory usage, and awk is used to calculate the percentage of used memory. Finally, pgrep -x checks if a process with the exact name specified exists.

The mail command sends the alert. For this to work, you’ll need a mail transfer agent (MTA) like postfix or sendmail configured on your system.

One subtle point often missed is how pgrep -x operates. The -x flag ensures an exact match for the process name, preventing partial matches. For example, if you’re monitoring nginx and have nginx-worker running, pgrep nginx would match both, but pgrep -x nginx would only match nginx itself.

The next logical step after basic monitoring is to implement more sophisticated error handling and perhaps integrate with a centralized logging system.

Want structured learning?

Take the full Bash course →