Monday , April 22 2024

shell script to check disk space and send email alerts

This article contain infromation about shell script to check disk space and send email alerts in linux. I wrote this article for regularly checking the disk usage rates of the PostgreSQL databases that I manage.

You can use the script which I will share to send disk usages to yourself as email for linux systems.

First, you should create a sh file in a directory on your system with the command below.

Then, by pressing the insert from the keyboard, paster the following contents to the file.

In the following script I have listed the parameters you need to change.

yourdiskpartYou should type the disk or mount name. For example /sda1 or /postgres
[email protected]The email address you want to send the mail to.
80When the Disk Usage Ratio exceeds 80%, it sends email.You can change this number according to your needs.
[email protected]The email address you want to send the mail from.
We copy the above contents into the file “” and then press Escape and exit with the :wq command.

You must run the following command to grant execute to the file.

We will then enter crontab with the command crontab -e and create a job that will run this script every morning at 09:00.

You can access the details of crontab job creation in the article titled “How To Create Crontab Job On Linux“.

After running crontab -e, we press the insert from the keyboard and paste the following content.

With this line, we will indicate that the file “” will work every day at 09:00 am.

Every morning at 09:00, you will be notified by an email if the disk usage rate of the partition you specified in the file is over 80%.

If it can not send the email even if the disk usage rate exceeds 80%, it means that you have done something wrong. You can try to solve the problem by running manually as follows.

If there is a problem with sending mail in linux, you can find the problem by using the article named “How To Check Crontab Job Log on Linux“.


About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *