Tech Tip: Send an Email Alert When Your Disk Space Gets Low


If you don't want to step up to a full monitoring solution such as Nagios you can create your own scripts for monitoring the things that you want to monitor, such as disk space. The following script alerts you when your root partition is almost full:

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%

The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD varialbe (90% here).

To run it daily, for example, save the script to the file in your home directory, change the email to your email, and add the following line at the end of /etc/crontab file:

@daily ~/


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

When i execute the script i get ./ Command not found

Anonymous's picture


I have created the script and put it in my home directory , When i excute the script i get the following error

./ Command not found.

Am not sure what's causing this


# set alert-level 90 % standard
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while r
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]; then
echo "space low on \"$partition ($usep%)\", on server $(hostname) at $(date)
mail -s "Alert: Free space low, $usep % used on $partition" $ADMIN

There are some similar scripts available

Anonymous's picture

It's a very basic topic for linux administration. There are some similar scripts available at, this site is dedicated for system monitoring and administration.

There are some problems with

T-One's picture

There are some problems with filesystems like cdrom drives, proc and stuff like that, so it would be better to filter that stuff, thats the script i use:

# set alert-level 90 % standard
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output;
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "space low on \"$partition ($usep%)\", on server $(hostname) at $(date)" |
     mail -s "Alert: Free space low, $usep % used on $partition" $ADMIN

if you use long devicenames you have to use df -p instead of df -h

Is it possible to set the

Supernoob's picture

Is it possible to set the execution of the script at a specific hour of the day?


Yes, see these

Mitch Frazier's picture

Check these: cron video and cron article.

Mitch Frazier is an Associate Editor for Linux Journal.


CyberCowboy's picture

on Ubuntu Karmic the 2nd line needs to read like this

CURRENT=$(df / | grep / | awk '{ print $4}' | sed 's/%//g')

Note the change from 5 to 4 in the awk portion of the line.

Perhaps check all

David Tangye's picture

Perhaps check all automounted filesystems ?

for vFS in `awk '!/^#/{if ($6 > 0) print $2}' /etc/fstab`
 do df $vFS  | \
 awk -v THRESHOLD=90 '
   if (VUSE >= THRESHOLD) {print VUSE " " $6}}'
 done | \
mail -e -s 'Disk Space Alert'

Here you do not need grep and sed

Andreas Schamanek's picture

If you use _awk_ you can make it also do what _grep_ and _sed_ do. An easy way is

CURRENT=$(df / | awk '/\// { print strtonum($5)}')

More correct would be

CURRENT=$(df / | awk '/\// { sub(/%$/,"",$5); print $5}')

But even cooler is the combination of df, awk and mail to get an alert for any file system above THRESHOLD:

df | awk "{ df=strtonum(\$5); if (df > $THRESHOLD) print; }" \
mail -e -s 'Disk Space Alert'

This basically all that is needed. _mail -e_ does not send a mail if the body is empty, i.e. when no file system is above THRESHOLD.


total noob question

Anonymous's picture

That's great, but how do I set things up so I can email from the command line?

> set things up so I can email from the command line

lefty.crupps's picture

You need to install a package which contains the 'mail' executable ('mail' program). On most systems that I've dealt with, this 'mail' command comes with the base install or comes as a part of an email transport system, such as Exim4 or Sendmail.

Try running this; the [Ctrl][D] key-combo tells the app when you're done composing:

shell$  mail -s "Subject here, hello there!" [enter]
whoa, ok i am sending an email! [enter]


Cc: [enter]


If you have long device

Anonymous's picture

If you have long device names like:


you have to use:

df -P (for POSIX output)

to ensure one line per device output.
Otherwise your example does not work in every situation.

One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix