Take the 2-minute tour ×
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.

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
add comment

2 Answers 2

up vote 1 down vote accepted

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(res.replace("temp=","").replace("'C\n",""))

# 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(line.split()[1:4])

# 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:
            return(line.split()[1:5])
share|improve this answer
add comment

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

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  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
  fi
done

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 at 13:29
add comment

Your Answer

 
discard

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.