Check free disk space in Linux
With a simple shell script, you can monitor your server's free disk space and send an email alert if it exceeds a threshold.
#!/bin/bash
# A small shell script to check disk and inode usage
FROM_EMAIL=[email protected] # The sender of the alert email
TO_EMAIL=[email protected] # The recipient of the alert email
LIMIT=90 # Percent (when to send email)
HOSTNAME=`hostname -f`
# Check all mount points
for i in `df |awk '{print $5}'|grep -v Use|sed 's/\%//'` ; do
if [ $i -gt $LIMIT ];then
(/usr/bin/printf "From: $FROM_EMAIL\nTo: $TO_EMAIL\nSubject: Disk alert on $HOSTNAME\n\n" ; echo "Disk is $i percent full") | /usr/sbin/sendmail -f$FROM_EMAIL $TO_EMAIL
fi
done
for i in `df -x vfat -x fat -i |awk '{print $5}'|grep -v Use|sed 's/\%//'` ; do
if [ $i -gt $LIMIT ];then
(/usr/bin/printf "From: $FROM_EMAIL\nTo: $TO_EMAIL\nSubject: Disk alert on $HOSTNAME\n\n" ; echo "Inode use $i percent") | /usr/sbin/sendmail -f$FROM_EMAIL $TO_EMAIL
fi
doneCrontab example
Last updated
Was this helpful?