• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools
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
October 16, 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
41
VIEWS
Share on FacebookShare on Twitter
Table of Contents
  • Monitoring Disk Usage with Email Alert with a Bash Script
    • Why do I need this script?
    • Key Components of the Script
    • Script for Monitoring Disk Usage with Email Alert

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.

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:

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

About The Author

neo

See author's posts

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

Related Posts

Automatic Windows Service Monitoring and Restart Script
Solutions

Automatic Windows Service Monitoring and Restart Script

Host a Website on Raspberry Pi – Real Benchmark Tests
Solutions

Host a Website on Raspberry Pi – Real Benchmark Tests

My Ultimate Docker Command Cheat Sheet (2025 Edition)
Docker

My Ultimate Docker Commands Cheat Sheet (2025 Edition)

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script
Docker

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

Raspberry Pi Monitoring with Monit: Docker, Temperature, Network & More
Solutions

Raspberry Pi Monitoring with Monit: Docker, Temperature & More

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)
Docker

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Next Post
How to Check Time and Change Timezone in Ubuntu

How to Check Time and Change Timezone in Ubuntu

Recommended

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

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

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

Image Metadata Remover – Clean EXIF and GPS Info Securely

Image Metadata Remover – Clean EXIF and GPS Info Online

Categories

  • 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
  • Web Tools
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

Recent News

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Online WebP to JPG Converter

Online WebP to JPG Converter

  • 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
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools

© 2025 defencedev.com - All rights reserved.