Disk Space Warning Script

monitorcron-readydf
4 min read

Quick Answer

The df command reports disk space usage for every mounted filesystem as a percentage. This script reads the percentage for a configurable partition (default: /), compares it against a threshold you set, and prints a warning to stdout — plus exits with code 1 — when that threshold is crossed. A full disk stops your web server from writing access logs, prevents your database from flushing transactions, and can corrupt in-progress writes. Without monitoring, the first sign of a full disk is often a crashed service, not an alert. The default threshold of 80% on a 25 GB VPS means you have roughly 5 GB remaining before trouble starts. Works on Ubuntu 22.04 LTS, Debian 12, Fedora 39, CentOS 9, and macOS Ventura — any system with bash and df. Run manually or schedule with cron every hour: 0 * * * * /home/user/diskcheck.sh.

The Script

Paste this into a file called diskcheck.sh and you're set. Change THRESHOLD to whatever percentage you want to be warned at.

bash
#!/bin/bash # diskcheck.sh — warn when disk usage exceeds a threshold THRESHOLD=80 PARTITION="/" USAGE=$(df "$PARTITION" | awk 'NR==2 {print $5}' | tr -d '%') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "WARNING: Disk usage on $PARTITION is at ${USAGE}% (threshold: ${THRESHOLD}%)" exit 1 else echo "OK: Disk usage on $PARTITION is at ${USAGE}%" exit 0 fi

What this does, line by line

  • THRESHOLD=80 — Alert when usage goes above 80%. Change this to match your policy.
  • PARTITION="/" — Which mount point to check. Use /var for a web server with large logs, or /home for user data.
  • df "$PARTITION" — Reports disk space for that partition.
  • awk 'NR==2 {print $5}' — Grabs the Use% column from the second line of df output.
  • tr -d '%' — Strips the percent sign so you can compare numbers in the if test.
  • [ "$USAGE" -gt "$THRESHOLD" ] — Bash integer comparison. Exits with code 1 on warning (useful for cron alerting).

Step-by-Step Setup

Step 1: Create the script file

bash
nano diskcheck.sh

Paste the script above. Press Ctrl+X, then Y, then Enter to save.

Step 2: Set your threshold

ThresholdBest for
70%Early warning on busy servers — gives you time to clean up before it's critical
80%General-purpose default — good balance for most Linux VPS and home servers
90%Last-chance alert — only when you want minimal noise and will act immediately

Edit the THRESHOLD= line at the top of the script to match your choice.

Step 3: Make it executable

bash
chmod +x diskcheck.sh

Step 4: Run it manually

bash
./diskcheck.sh

When usage is below the threshold:

text
OK: Disk usage on / is at 42%

When usage is above the threshold:

text
WARNING: Disk usage on / is at 87% (threshold: 80%)

Schedule It with Cron

Running the script by hand only helps when you remember. For a server, put it on a schedule with cron so you catch problems early.

Open your crontab:

bash
crontab -e

Add one of these lines (adjust the path to where you saved diskcheck.sh):

bash
# Every hour at minute 0 0 * * * * /home/you/diskcheck.sh # Every day at 8:00 AM 0 8 * * * /home/you/diskcheck.sh # Every 5 minutes (aggressive — use on critical production disks only) */5 * * * * /home/you/diskcheck.sh

Use the full path in cron

Cron runs with a minimal environment. Always use the full path to your script (e.g. /home/you/scripts/diskcheck.sh), not ./diskcheck.sh. If the script calls other commands, set PATH at the top of the script or use full paths for those too.

Variations

Check multiple partitions

Use this version when you need to monitor /, /var, and /home in one run:

bash
#!/bin/bash THRESHOLD=80 PARTITIONS=("/" "/var" "/home") for PARTITION in "${PARTITIONS[@]}"; do if ! mountpoint -q "$PARTITION" 2>/dev/null; then echo "SKIP: $PARTITION is not mounted" continue fi USAGE=$(df "$PARTITION" | awk 'NR==2 {print $5}' | tr -d '%') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "WARNING: $PARTITION is at ${USAGE}%" else echo "OK: $PARTITION is at ${USAGE}%" fi done

Save as diskcheck-multi.sh, then chmod +x diskcheck-multi.sh.

Send email alert instead of echo

bash
#!/bin/bash THRESHOLD=80 PARTITION="/" EMAIL="admin@yourdomain.com" USAGE=$(df "$PARTITION" | awk 'NR==2 {print $5}' | tr -d '%') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "Disk usage on $PARTITION is ${USAGE}% (threshold ${THRESHOLD}%)" | \ mail -s "Disk alert: $PARTITION at ${USAGE}%" "$EMAIL" fi

Requires mailutils (Debian/Ubuntu: sudo apt install mailutils). Save as diskcheck-email.sh.

Common Mistakes

Checking the wrong partition

On many servers, /var or /home fills up before /. If you only run df /, you can miss a full log or upload directory. Match PARTITION to where data actually grows — e.g. PARTITION="/var" for Apache/nginx logs.

Script warns but nobody sees it

Cron runs in the background. If you only echo to stdout, output may go to email (if MAILTO is set) or nowhere. Redirect output to a log you check:

bash
0 * * * * /home/you/diskcheck.sh >> /var/log/diskcheck.log 2>&1

Or use the email variation above.

df output differs by OS

On macOS, df can wrap columns differently. Use POSIX mode for consistent parsing:

bash
USAGE=$(df -P "$PARTITION" | awk 'NR==2 {print $5}' | tr -d '%')

The -P flag forces POSIX output (one line per filesystem).

Understanding the Commands

CommandWhat it does
df /Disk free — shows used/available space for the root filesystem
df -h /Same as df / but human-readable sizes (GB, MB)
awk 'NR==2{print $5}'Prints field 5 from line 2 — the Use% column
tr -d '%'Deletes % characters so 85% becomes 85 for numeric compare
[ "$USAGE" -gt "$THRESHOLD" ]True when usage is greater than your threshold

Frequently Asked Questions

How do I check disk space in Linux with bash?

Use df (disk free). For a quick human-readable check:

bash
df -h /

To extract just the usage percentage for scripting:

bash
df / | awk 'NR==2 {print $5}'

Combine with THRESHOLD and an if statement — that's what diskcheck.sh does.

How do I get an email when my disk is full?

  1. Install mail tools: sudo apt install mailutils
  2. Use the email variation script with your address in EMAIL=
  3. Schedule with cron so it runs hourly or daily

The mail command sends the script output as the message body.

How do I get an alert when disk space is low?

Bash + threshold + cron is the standard pattern: this script compares usage to THRESHOLD, prints or mails on breach, and cron runs it on a schedule. For SMS or Slack, pipe the warning into curl to a webhook instead of echo.

How do I send an email alert from a bash script?

bash
echo "Your message body" | mail -s "Subject line" user@example.com

Use a meaningful subject (Disk alert: / at 87%) so you can filter inbox rules.

What does the df / command do in Linux?

df means disk free. It reports filesystem size, used space, available space, and mount point. The / argument limits output to the root filesystem. Columns are typically: Filesystem, 1K-blocks, Used, Available, Use%, Mounted on.

BashSnippets logo

Written by Anguishe

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Run this script on a real Linux server

Get $200 free credit — DigitalOcean

Get $200 Free →

Affiliate link · we earn a commission

Need a domain for your next project?

Register with Namecheap — free WHOIS privacy included

Check Domain Prices →

Affiliate link · we earn a commission

Related Snippets

Frequently Asked Questions

How do I run this script?

Save as diskcheck.sh, run chmod +x diskcheck.sh, then execute ./diskcheck.sh. Exit code 1 means usage exceeded your THRESHOLD.

Does this work on macOS?

Yes. Use df -P for POSIX output — macOS df wraps columns differently without the -P flag.

How do I check disk space in Linux with bash?

Use df / and awk to extract the Use% column. This script compares it against THRESHOLD and exits 1 on breach.

How do I get an email when my disk is full?

Install mailutils with sudo apt install mailutils, set EMAIL in the email variation script, and schedule with cron hourly.