Example:
You would like to receive an email alert when your disk space gets low. For this, perform a quick Google search for, say, “Send an Email Alert When Your Disk Space Gets Low”. Amongst the various result, you will see the one from Linux Jornal. Click the URL and you will see the bash script documented on the page.
Create a new text file using nano
for the bash script:
$ nano monitor_disk_space.sh
Copy and paste the contents from the URL:
#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' mailid@domainname.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
Note: Please do not forget to replace mailid@domainname.com
with your e-mail address. Also, please remember that you can modify the subject line as well.
Press “CTRL+X” and confirm with “Y” in order to save and exit the file.
You have now created a small bash program called monitor_disk_space.sh
which you can name it as you like.
We need to continue with telling our operating system that this file is an executable.
Give the file executable permission using “chmod”:
$ chmod +x monitor_disk_space.sh
You can try to run the file by executing it: ./monitor_disk_space.sh
Given that we would like this small program to act like a system monitor, we will need to use the utility tool cron to schedule it to run at certain intervals.