Nightly mail with disk and ram usage

Written by Robert -

I'm maintaining Linux servers and I want to know what the disk and ram usage is. I want to get this in my mailbox every night to prevent system issues with disk usage. To do this, I created a simple batch script. Feel free to copy this and use it yourself.


#!/bin/bash  
echo This is the disk usage on $HOSTNAME > /tmp/mail_report.log  
# This line adds a comment regarding the next command 
# and it's hostname and adds it to a new file called mail_report.log in the /tmp folder  
# The $HOSTNAME variable is used in Linux as the hostname of the server.  
# Keep in mind that any old file (so in this case from the day before), 
# is overwritten with a new file  
# This is due to the singe > symbol  
# The line is added for readability  
 echo >> /tmp/mail_report.log  
# This creates an empty line in the mail_report.log file df -h >> /tmp/mail_report.log  
# This adds the output of df -h to the mail_report.log file   
echo >> /tmp/mail_report.log  
echo >> /tmp/mail_report.log  
echo >> /tmp/mail_report.log  
# This creates 3 empty lines to the mail_report.log file   
echo ram usage on $HOSTNAME >> /tmp/mail_report.log  
# This adds a line regarding the next command to the mail_report.log, for readability.   
echo >> /tmp/mail_report.log free -m >> /tmp/mail_report.log  
# This adds a line with the memory usage to the mail_report.log file   
echo >> /tmp/mail_report.log mail -s "disk and ram report $HOSTNAME " user@domain.com < /tmp/mail_report.log   
# -s specifies the subject  
# after that comes the mail address  
# the < is for inputting the /tmp/mail_report.log file  



I created a file with the content above and named it report.sh

After that, I made the file executable by using the chmod +x command. server:~ # chmod +x report.sh

Test the functionality of the created file. server:~ # ./report.sh

Add it to the crontab. server:~ # crontab -e

You can either:

Use it in the "old" way by using the minute / hour / day / month / year system.
In this case I want to run it at 2 AM daily. As a result I have to specify it as minute 0, hour 2, any, any, any command. This looks like:
0 2 * /root/report.sh
If you run this on a recent system you easily run it at midnight by using
@midnight /root/report.sh
See the man page for crontab if you wish to know more options.

Comments