Shell script to find disk space

Respected Readers,

This post is about getting email alerts when application disk space crosses a threshold. It is a common requirement to get alerts so that one can be proactive rather than reactive. Recently, I was asked to write such a process.

filesystem=`df -bhk <unix-path> |awk '{print$1}'`
spaceallocated=`df -bhk <unix-path> |awk '{print$2}'`
spaceused=`df -bhk <unix-path> |awk '{print$3}'`
spaceavailable=`df -bhk <unix-path> |awk '{print$4}'`
capacity=`df -bhk <unix-path> |awk '{print$5}'`
mountedon=`df -bhk <unix-path> |awk '{print$6}'`
space=`df -hk <unix-path> |awk '{print$5}'`
space=`echo $space | sed 's/capacity//g'`
space=`echo $space | sed 's/%//g'`
echo $space
if test $space -ge 95
echo $filesystem "\n" $spaceallocated "\n" $spaceused "\n" $spaceavailable "\n" $capacity "\n" $mountedon | mailx -s "Low Disk Space Alert"

1) df command returns summary of free disk space

df -h <unix-path>

2) Threshold in above script is set to 95. It can be changed as required by application.

This script can be added to cron to run every 1 hour. The script was tested with threshold of 50%. I hope it alerts when the usage passes 95%. Otherwise my manager is going to hunt for me (haha, just kidding).



About Badal Chowdhary

I am a Software Engineer by profession. I have done SCJP and SCWCD certifications. Like working on cutting edge technologies and frameworks. Driven by challenges and fascinated by technology. I love playing and watching sports: Cricket, Ping Pong, Tennis, Badminton, Racket Ball and Gym.
This entry was posted in Unix and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s