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!

2 Answers 2


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%" [email protected]

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.


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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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