UNIX Get An Alert When Disk Is Full

I want to get an alert when my disk is full under UNIX and Mac OS X? How do I set a a specified threshold and run the script via cron?

The df command report file system disk space usage including the amount of disk space available on the file system containing each file name argument. Disk space is shown in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

Advertisement

Use df command and pass the -P option which make df output POSIX compliant (i.e. 512-byte blocks rather than the default. Note that this overrides the BLOCKSIZE specification from the environment).
# df -P /
OR
# df -P /usr
Sample Outputs:

Filesystem    512-blocks     Used     Avail Capacity  Mounted on
/dev/aacd0s1e  162491344 21988048 127503992    15%    /usr

You can now simply grep /usr file system and print out used capacity:
# df -P /usr | grep /usr | awk '{ print $5}' | sed 's/%//g'
15
Or assign value to a variable:
# output=$(df -P /usr | grep /usr | awk '{ print $5}' | sed 's/%//g')
# echo $output

Under BASH or KornShell you can use arrays indexed by a numerical expression to make code small:
# output=($(df -P /))
# echo "${output[11]}"

A Sample Shell Script

#!/bin/bash
# Tested Under FreeBSD and OS X
FS="/usr"
THRESHOLD=90
OUTPUT=($(LC_ALL=C df -P ${FS}))
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
[ $CURRENT -gt $THRESHOLD ] && echo "$FS file system usage $CURRENT" | mail -s "$FS file system" you@example.com

You need to modify syntax, if you are using KSH or TCSH / CSH instead of BASH. Save this script and run as a cron job:
@daily /path/to/your.df.script.sh

GUI Notification

Display warning dialog using /usr/bin/zenity

#!/bin/bash
# Tested Under FreeBSD and OS X
FS="/usr"
THRESHOLD=90
OUTPUT=($(LC_ALL=C df -P ${FS}))
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
[ $CURRENT -gt $THRESHOLD ] && /usr/bin/zenity  --warning  --text="The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk." --title="df Warning"
DF GUI Warning Notification

DF GUI Warning Notification

Finally update your cronjob as follows (you need to use DISPLAY variable to display output window):

36 19 * * *  DISPLAY=:0.0 /path/to/script.sh

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

28 comments… add one
  • Jeroen Aug 15, 2009 @ 22:20

    Hai Vivek and petrescs,

    The updating of the user cronjob worked fine so I changed my user crontab with the “DISPLAY=:0.0” . I am not sure what the extra value is of the /etc/profile.d/run.cron.gui.sh. With or without it my shell script starts and finishes and produces ( as far as I can see ) the same results.

    This “DISPLAY=:0.0” added to the cronjob solved for me other problems that output could not be showed on the display and gui applications could not be executed.

    Thank you @petrescs and @vivek for the answers to my questions. They were very helpful.

    Jeroen

  • Mark Shields Nov 13, 2011 @ 17:37

    No need to use grep and awk together, at least not in the context you’re using it. And sed is a bit of an overkill; use tr instead.

    df -P /usr | awk '/\/usr/{ printf $5}' | tr -d "%"
  • Randall Oveson Mar 6, 2012 @ 21:07

    Thanks for the script, works great!

  • aj Nov 19, 2012 @ 15:16

    Hello,
    I using this scripts and i am getting this following error

    # vi full.ksh
    “full.ksh” 7 lines, 377 characters
    #!/bin/ksh
    set -x
    # Tested Under FreeBSD and OS X
    FS=”/usr”
    THRESHOLD=90
    OUTPUT=($(LC_ALL=C df -P ${FS}))
    CURRENT=$(echo ${OUTPUT[11]} | sed ‘s/%//’)
    [ $CURRENT -gt $THRESHOLD ] && /usr/bin/zenity –warning –text=”The disk $FS ($CURRENT% used) is almost full. Delete some files or add a new disk.” — ti
    tle=”df Warning” | mail -s “$FS file system” ak@mountsaintvincent.edu

    # sh full.ksh
    FS=/usr
    THRESHOLD=90
    full.ksh: syntax error at line 6: `OUTPUT=’ unexpected

  • Joe Bazin Nov 23, 2013 @ 21:01

    I am looking for shell script which can be scheduled as cronjob under root to do the following on server named abcz on AIX platform.

    The script should capture the unsuccessful login attempts for user ID joebazin & oracle with the details -ip address, attempt time and number of attempts.

  • foobar Oct 28, 2014 @ 22:57

    Use -P for POSIX standard:

    $ df -P | awk ‘/\/home$/{ gsub(/%/,””); print $5 }’

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.