Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help in creating a script that will run once a day in afternoon and collect the information of each partition(Disk Space , Total and Used) and send it to my email.

Please help im very very new in this scripting thing.

share|improve this question
up vote 2 down vote accepted

Provided you have an MTA configured on that machine to accept and relay mail for you (on a server that should be the case) try this:

$ df -h | mail -s "Filesystem usage report for `hostname`" myemail@domain.tld

(MTA = Postfix, Exim, etc.)

Example emailed report

If that fits your needs, add it to your crontab to run every day:

$ crontab -e

An editor will open. Add a line like this:

@daily df -h | mail -s ...

Save and close.

This will make it run with the other daily tasks. If you need a report on a specific time of the day or error logging to a specific address, please read about the cron syntax (a lot of this is on the Internet - here's one random website). For example:

# at 5 a.m every day:
0 5 * * * mycommand

In case you can't send out mail on that machine directly, read this or the answer by @hakermania on how to do that (many more ways exist).

share|improve this answer
I have not installed any mail server on my TEST PC. Can u please provide any good resource that can be used to install Posfix or Exim. I have searched for Posfix installation. most of them are very confusing . Which one should I install Postfix, Exim or Sendmail. Thanks – OmiPenguin Sep 16 '12 at 11:37
@UmairMustafa If it's just a PC and you're unfamiliar with MTA configurations I recommend you to use a script that uses authentication for a remote SMTP server instead. A lot depends on your situation - like whether you have a mail relay server in your network already. – gertvdijk Sep 16 '12 at 11:41
Situation is this that I will first try it on my Test PC then later I will assign the same Script to each server in company running on linux. So tell me do I have to install Postfix first . If yes then I would really appreciate if u provide some assistance. – OmiPenguin Sep 16 '12 at 13:19
@UmairMustafa Configuring your mail gateway and relays is a completely differrent question. Try asking that kind of question on – gertvdijk Sep 16 '12 at 13:22

In order to make the script running you will need to setup a cron job with it: How do I setup a cronjob ?

Now, inside your script, you will need to do something like this:

#script that simply saves the output of df -h to an output file
#which is sent as an attachment to an e-mail

#a) save the output of the command:

df -h > $temp_file 2> /dev/null

/root/ "Title here" "Body here. The current date and time is $(date)" "$temp_file"

sleep 3
rm -rf $temp_file

As you can see, I'm calling a python script from within your root path (not readable but nobody else but root himself) which takes the following arguments:

"recipient-email" "title-of-email" "body-of-email" "attachment"

This python script is this:

import os, re
import sys
import smtplib

from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.MIMEText import MIMEText


sender = ''
password = "yourpasswordhere"
recipient = sys.argv[1]
subject = ''
message = sys.argv[3]

def main():
    msg = MIMEMultipart()
    msg['Subject'] = sys.argv[2]
    msg['To'] = recipient
    msg['From'] = sender

    part = MIMEText('text', "plain")

    session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)


    session.login(sender, password)

    fp = open(sys.argv[4], 'rb')
    msgq = MIMEBase('audio', 'audio')
    # Encode the payload using Base64
    msgq.add_header('Content-Disposition', 'attachment', filename=filename)
    # Now send or store the message
    qwertyuiop = msg.as_string()

    session.sendmail(sender, recipient, qwertyuiop)

    os.system('notify-send "Your disk space related email has been sent."')

if __name__ == '__main__':

Of course you will need to provide it with your gmail email and password at the top of the script (sender and password variables). If you install the libnotify-bin package, then, if the email was successfully sent, you will be notified with a desktop notification.

So, summing up, you will need to setup a cron job with the above bash script. This bash script will save the output of df -h inside a temporary file, which will be sent via a python script to a recipient email of your choice (as far as I remember, the sender email must be gmail).

PS: The above solution will show the free and total disk space only of your mounted filesystems. If this is a problem please inform me so as to extend my answer about how to automatically mount all the available filesystems and then run df -h.

share|improve this answer
I recommend using the sendemail package for sending mails to remote SMTP servers directly instead. – gertvdijk Sep 18 '12 at 1:17
@gertvdijk please feel free to edit the answer and include yours as well, pointing out why sendemail may be a better option. Or add it as an answer! – hakermania Sep 18 '12 at 1:59

You can use this script to check the disk usage


out=`df -k | grep "^/dev" | awk '{ if($5 > $limit) print "\nDisk space is critial on " $1,$5,$6 "\n"}'`
usr/bin/mail -s "Disk Space Alert on $host: $out" $email

Use cron to make the script run automatically. Check this online cron generator for helping you in setting it up.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.