How To Send Disk Usage as email On Linux

 

I wrote this article for regularly to see the disk usage rates of the PostgreSQL databases that I manage.

You can use this article to send usage of disks on linux systems to yourself as mail.

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

 

Then, by pressing the insert from the keyboard, you should transfer the following contents to the file.

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

yourdiskpart You should type the disk or mount name. For example /sda1 or /postgres
x@xyz.com The email address you want to send the mail to.
80 When the Disk Usage Ratio exceeds 80%, it sends by mail.

 

You can change this number according to your needs.

y@xyz.com The email address you want to send the mail from.

 

 

We copy the above contents into the file “diskusage.sh” 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 “diskusage.sh” will work every day at 09:00 am.

 

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

If the mail does not arrive at the time it should come, it means that you have done something wrong. you can try to solve the problem by running diskusage.sh manually as follows.

 

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