• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Solutions

Solution: Monitoring Disk Usage with Email Alert Using a Bash Script and Crontab on Linux

neo by neo
May 27, 2025
in Solutions
0
Solution: Monitoring Disk Usage with Email Alert Using a Bash Script and Crontab on Linux

Solution: Monitoring Disk Usage with Email Alert Using a Bash Script and Crontab on Linux

0
SHARES
15
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Monitoring Disk Usage with Email Alert with a Bash Script

Running out of disk space can lead to system crashes, data loss, or degraded performance, especially in a production environment. Proactive monitoring of disk usage is essential, but manually checking available space isn’t practical for busy administrators. Automating the process is a smart and efficient way to stay ahead of potential issues.

In this article, we’ll show you how to create a simple powerful Bash script to monitor disk usage on a Linux system. With the help of crontab, this script will run at scheduled intervals and send email alerts whenever disk space crosses a specified threshold. This solution ensures you’re always informed about your system’s storage health, allowing you to take timely action and prevent critical issues.

Why do I need this script?

Maintaining optimal disk usage is crucial to ensure system performance and prevent downtime. High disk usage can lead to a multitude of problems, including slow performance, system crashes, and data loss. To mitigate these risks, it’s essential to monitor disk usage regularly and take proactive measures.

ADVERTISEMENT

Key Components of the Script

  • Defining Email Addresses: At the beginning of the script, we define the email addresses for sending alerts. This includes the sender’s email and the recipient’s email.
FROM_EMAIL="email1@domain.com"
TO_EMAIL="email2@domain.com"
  • Setting the Threshold: We set the threshold for disk usage. In this example, the threshold is set to 85%, but you can adjust it according to your requirements.
THRESHOLD=85
  • Fetching Disk Usage Information: The script fetches the current disk usage percentage, disk name, hostname, IP address, and available disk memory.
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
DISK_NAME=$(df -h / | awk 'NR==2 {print $1}' | sed 's/%//')
HOSTNAME=$(hostname)
IP=$(hostname -I | awk '{print $1}')
  • Logging and Alerting: If the disk usage exceeds the threshold, the script logs the information and sends an alert email to the specified recipient.
if [ "$DISK_USAGE" -gt "$THRESHOLD" ]; then
LOG_FILE="/opt/scripts/log/disk_usage.log"
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
MESSAGE="Disk usage on server ${HOSTNAME} (IP:${IP}), disk name ${DISK_NAME} (capacity: ${DISK_MEMORY_CAPACITY}, free space: ${DISK_MEMORY_FREE}, used: ${DISK_MEMORY_USED}) is above ${THRESHOLD}%! Current usage is ${DISK_USAGE}%. Script executed at ${TIMESTAMP}."
echo "$MESSAGE" >> "$LOG_FILE"
subject="ALERT! Service: check_disk - $(date +%Y-%m-%d)"
echo "$MESSAGE" | mail -a "From: ${FROM_EMAIL}" -s "$subject" ${TO_EMAIL}
fi

Script for Monitoring Disk Usage with Email Alert

  • Create folders for the script and logging
mkdir -p /opt/scripts/log/
  • Copy the content of the script in the file:
nano alert_mail_check_disk.sh

Content of the file:

#!/bin/bash

# Define email addresses
FROM_EMAIL="email1@domain.com"
TO_EMAIL="email2@domain.com"

# Set the threshold for disk usage (85% in this case)
THRESHOLD=85

# Get the current disk usage percentage for the root filesystem
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
DISK_NAME=$(df -h / | awk 'NR==2 {print $1}' | sed 's/%//')
HOSTNAME=$(hostname)
IP=$(hostname -I | awk '{print $1}')

# Get the available disk memory
DISK_MEMORY_FREE=$(df -h | awk '$NF == "/" {print $4}')
DISK_MEMORY_USED=$(df -h | awk '$NF == "/" {print $3}')
DISK_MEMORY_CAPACITY=$(df -h | awk '$NF == "/" {print $2}')
DISK_MEMORY_USED_PROCENT=$(df -h | awk '$NF == "/" {print $5}')

# Check if disk usage exceeds the threshold
if [ "$DISK_USAGE" -gt "$THRESHOLD" ]; then
  # Log the information
  LOG_FILE="/opt/scripts/log/disk_usage.log"
  TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
  MESSAGE="Disk usage on server ${HOSTNAME} (IP:${IP}), disk name ${DISK_NAME} (capacity: ${DISK_MEMORY_CAPACITY}, free space: ${DISK_MEMORY_FREE}, used: ${DISK_MEMORY_USED}) is above ${THRESHOLD}%! Current usage is ${DISK_USAGE}%. Script executed at ${TIMESTAMP}."
  echo "$MESSAGE" >> "$LOG_FILE"
  subject="ALERT! Service: check_disk - $(date +%Y-%m-%d)"
  echo "$MESSAGE" | mail -a "From: ${FROM_EMAIL}" -s "$subject" ${TO_EMAIL}
fi
  • Make a file executable:
chmod +x alert_mail_check_disk.sh

Crontab Installation

  • Edit the crontab to execute script automaticaly:
crontab -e
  • Add the following line in the Crontab to check disk usage once per day at 22.00h
# Check disk usage every day at 22.00h
00 22 * * * bash /opt/scripts/alert_mail_check_disk.sh

If you want to do more checks during the day, you can do it by changing the parameters. Check the following link to create your own schedule.

Example to check disk usage every 15 Minutes:

# Check disk usage every 15 Minutes
*/15 * * * * bash /opt/scripts/alert_mail_check_disk.sh

ADVERTISEMENT

About The Author

neo

See author's posts

ADVERTISEMENT
Tags: bash-scriptingmain-news
ADVERTISEMENT
Previous Post

YouTube Channel about Linux Topics

Next Post

How to Check Time and Change Timezone in Ubuntu

neo

neo

Next Post
How to Check Time and Change Timezone in Ubuntu

How to Check Time and Change Timezone in Ubuntu

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

Install SoftEther VPN Server on Ubuntu 24.04

Install SoftEther VPN Server on Ubuntu 24.04

Recent News

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

Install SoftEther VPN Server on Ubuntu 24.04

Install SoftEther VPN Server on Ubuntu 24.04

Timeshift on Linux: How to Create and Restore System Snapshots

Timeshift on Linux: How to Create and Restore System Snapshots

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.