Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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 want to send an email when the space on my pi's sd card is getting low. Also I would like to delete old files when disk space is very full.

I can program in Python though I have not yet done so for the pi. I am sure sending an email would be easy in python; I would just need a way to check for disk space and delete the oldest files in python also. Are there any existing programs or scripts that do this well? Should I just write my own?

Thank you in advance!

share|improve this question
up vote 7 down vote accepted

here's a short version of bash script for your purpose:

df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge 90 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep%" you@somewhere.com

you have to install sendmail and setup a cron entry to run this script, according to a very detailed explanation. there's also an extended version of this script available at the same url.

share|improve this answer
Thanks, looks super simple! I will give it a try. – skyfly200 Apr 29 '14 at 13:29

You can use ssmtp and mailutils to send out emails. Just google and you will find some tutorials. You can use ssmtp and mailutils in python by using os.system() orsubprocess().

To check for file size, again, do a little googling. Here is one from here.

Here is a snippet:

import os

# Return CPU temperature as a character string                                     
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()

# Return RAM information (unit=kb) in a list                                       
# Index 0: total RAM                                                               
# Index 1: used RAM                                                                 
# Index 2: free RAM                                                                 
def getRAMinfo():
    p = os.popen('free')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:

# Return % of CPU used by user as a character string                               
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\

# Return information about disk space as a list (unit included)                     
# Index 0: total disk space                                                         
# Index 1: used disk space                                                         
# Index 2: remaining disk space                                                     
# Index 3: percentage of disk used                                                 
def getDiskSpace():
    p = os.popen("df -h /")
    i = 0
    while 1:
        i = i +1
        line = p.readline()
        if i==2:
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.