Skip to content

Instantly share code, notes, and snippets.

@fduran
Created February 20, 2012 18:15
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save fduran/1870429 to your computer and use it in GitHub Desktop.
Save fduran/1870429 to your computer and use it in GitHub Desktop.
Linux disk space email alert
#!/bin/bash
# www.fduran.com
# script that will send an email to EMAIL when disk use in partition PART is bigger than %MAX
# adapt these 3 parameters to your case
MAX=95
EMAIL=alert@example.com
PART=sda1
USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL
fi
@ifpingram
Copy link

fantastic script - thank you!

@mrforsythexeter
Copy link

I had to use
df -hP
To get posix format as the $PART was making normal output wrap to a new line.

@fy-kenny
Copy link

grateful

@onepoint0
Copy link

Excellent, thank you!

@atural91
Copy link

Hi I have similar script like yours:

if [ $USED -gt $LIMIT ]

#If used space is bigger than LIMIT

then

  du -sh ${DIR}/* | $MAILX -s "$SUBJECT" "$MAILTO"

#This will print space usage by each directory inside directory $DIR, and after MAILX will send email with SUBJECT to MAILTO

fi

But getting [: too many arguments error.

Could you please help?

@sadakura
Copy link

atural91 it won't work on newer distros of linux, have tried on Ubuntu 18.04, got the same error, plenty of software which starts with Ubuntu on boot and sends out emails, just search Github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment