Uncategorized / Shell Script for Disk Space Notification

  • Vimal Raj
  • 3578 Views
  • 0 Comment
  • No tags

Shell Script for Disk Space

Shell script for disk space

  • 1. Create a file /home/diskspace.sh
  • 2. Enter the following code in the script
    shell script

#!/bin/sh

#Threshold is set to 70 here.
THRESHOLD=70
MAILTO=”youremailaddress”
TEMPFILE=/tmp/diskspace.temp
HOSTNAME=`hostname`

 

rm -f $TEMPFILE

 

#Calculate the Current Disk Usage with the below command.
CDU=$(df -h | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)

#Compare the current value with the threshold one.
if [ $(expr $CDU “>=” $THRESHOLD) -ne 0 ] then
echo “Warning!!! Disk Space usage on server $HOSTNAME is ${CDU}%” >> $TEMPFILE
fi

#Send an email if /tmp/diskspace.temp is present.
if [ -e $TEMPFILE ] then
mail -s “Disk Space Notification” $MAILTO < $TEMPFILE
fi

rm -f $TEMPFILE

3. Save the file and execute using crontab

crontab -e

0 1 * * 0 /bin/sh /home/diskspace.sh

0 Comments
Leave a comment
OUR SERVICES
OUR TECHNOLOGIES