Friday, October 31, 2014

Send An Email Alert When Your Disk Space Gets Low

The following script alerts you when your root partition is almost full:
  1. Update the Server : - apt-get update
  2. Install mailx client :- apt-get install bsd-mailx
  3. Create a File disk.sh
  4. Add the Content
#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=9
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' ictops@gmail.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
                                      The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD variable (9% here).
Script automatically run when it added into the Crontab file
root@ubuntu:~# crontab -e
10 15 * * * /root/disk.sh (Execute the command at 3.10 pm everyday)

No comments:

Post a Comment

bloggerwidgets