Create Script to Check Disk Space and Send Email Alerts for Low Disk Space

Summary

Monitoring your hard disk is essential to avoid issues caused by low disk space. By creating a script to monitor the disk, you can receive email alerts when space is running low, helping you prevent system crashes and maintain optimal performance.

Andrew Wright

Updated on 2024/11/02

by Andrew Wright

How to Create a Script to Check Disk Space and Send Email Alerts

Running low on disk space on your C drive can cause significant problems. Your computer may slow down, you won’t be able to install new programs, and a full drive can even result in a system crash. To avoid these issues, you can set up a script to monitor disk space and configure it to send email alerts when space usage exceeds a defined threshold.

Below is a sample script you can use to monitor your disk space. The script checks if the disk usage exceeds 90%, and if so, sends an alert email. Simply replace the placeholder email address with your own.

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
THRESHOLD=90
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
    mail -s 'Disk Space Alert' mailid@domainname.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
    

To implement the script:

  1. Save the script to a file, for example, sample.sh, in your home directory.
  2. Make the script executable by running:
    chmod +x ~/sample.sh
  3. Schedule the script to run daily by adding the following line to the end of the /etc/crontab file:
    @daily ~/sample.sh

Once set up, you will receive an email alert whenever the disk space usage exceeds the threshold.

How to Free Up the C Drive When You Receive Low Disk Space Alerts

When your system drive is running out of space, your computer may slow down, get stuck during processes, or prevent you from downloading files or installing new programs. Here’s how you can free up space or enlarge the system drive:

1. Use Disk Management or Command Prompt: Try extending the volume using Disk Management or the diskpart command if you have unallocated space adjacent to the C drive.

2. Use Partition Management Tools: If you don’t have unallocated space next to the C drive, you can use a partition management tool like IM-Magic Partition Resizer Free. This tool is safe, reliable, and free for Windows systems.

Enlarge C drive on Windows with partition tool Enlarge C drive on Windows with partition tool

For Windows 11/10/8/7 => Download Partition Resizer Free [100% Free]

For Windows Server 2022-2003 => Download Partition Resizer Server [Free Demo]

Related Product