Skip to content

Disk Space Warning Script

monitorcron-readydf
6 min read
Matching toolCron Job Builder

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. df ships in coreutils on every Linux distribution and on macOS — nothing to install. Run manually or schedule with cron every hour: 0 * * * * /home/user/diskcheck.sh.

A long task on my own machine died halfway through with a write error, and only when I went looking did I run df and find the root SSD sitting at 100%. I'd been reacting to a crash that a five-line check would have warned me about an hour earlier. There was no heads-up, no yellow light — the disk went from "fine" to "out" while I wasn't looking, and the first I heard of it was a program falling over because there was nowhere left to write.

That's the nature of a full disk: it gives no warning on its own, and by the time something fails loudly enough to notice, you're already past the point where you had options. The fix isn't clever, which is the point — read the usage percentage off df, compare it to a threshold you choose, and say something while there's still room to act. Run it once and it tells you where you stand right now; run it on a schedule and it turns a silent wall you hit into a warning you get ahead of. The script below is the difference between finding out from a threshold and finding out from a crash. I'd rather get the warning than clean up after the wall.

What Does the Disk Space Warning Script Look Like?

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 set -euo pipefail 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).

How Do I Set Up and Run the Disk Space Warning Script?

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%)

How Do I Schedule This Script to Run Automatically?

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. Disk monitoring is one of several jobs every box needs from day one — the bash scripts every sysadmin needs guide covers the full baseline.

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.

What Are Common Variations of This Script?

Check multiple partitions

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

bash
#!/bin/bash # diskcheck-multi.sh — warn when any of several partitions exceeds a threshold set -euo pipefail 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 # diskcheck-email.sh — send an email alert when disk usage exceeds a threshold set -euo pipefail 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.

What Common Mistakes Break Disk Space Monitoring?

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. Once you identify the full partition, the find duplicate files script locates redundant copies consuming space with no extra tools.

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. For full SMTP setup, Gmail relay via msmtp, and send-once lockfiles that prevent inbox flooding, see the bash email alert script. Pair disk monitoring with CPU and RAM monitoring to cover all three resources from the same cron schedule.

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).

What Does Each Command in the Script Do?

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 to read usage and awk to pull out the Use% column: df / | awk 'NR==2 {print $5}' | tr -d '%'. That leaves a bare number you can compare against a threshold in an if test, which is exactly what the script above does before deciding whether to warn.

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

The standard pattern is bash plus a threshold plus cron: the script compares current usage to a number you set, prints or emails a warning on breach, and cron runs it on a schedule so you never have to remember. For SMS or Slack instead of email, pipe the warning line into curl against a webhook rather than echo.

Why does my disk fill up without any warning?

Because nothing on a stock Linux box watches free space for you. A filesystem reports its usage when asked and stays silent otherwise, so usage climbs invisibly until a write fails. A threshold check is the thing that turns "the disk is full" into "the disk is getting full" while you still have room to act.

What does the df / command do in Linux?

df means disk free. It reports filesystem size, used space, available space, and the mount point. The / argument limits the output to the root filesystem. The columns are Filesystem, 1K-blocks, Used, Available, Use%, and Mounted on — the script reads Use% from the second line.

Part of the Server Monitoring · Disk Management collection

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

PAID RESOURCE — $9

The Production Bash Toolkit

6 scripts + shared library + 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →

Related Snippets

Frequently Asked Questions

faq — snippet

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.

faq — snippet

Does this work on macOS?

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

faq — snippet

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.

faq — snippet

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.

faq — snippet

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.

faq — snippet

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

Use: echo "Your message body" | mail -s "Subject line" user@example.com — requires mailutils on Debian/Ubuntu. Use a meaningful subject so you can filter inbox rules.

faq — snippet

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: Filesystem, 1K-blocks, Used, Available, Use%, Mounted on.