个人工具

UbuntuHelp:EmptyDiskSpace

来自Ubuntu中文

跳转至: 导航, 搜索

<<Include(Tag/StyleCleanup)>>

Introduction

Did you ever wasted time investgating software problems or application crashes only to find out that it was caused by a full hard disk? The following simpel script warns if your hard disk runs out of available disk space.

Installation

1. Open a text editor with sudo to create the file /usr/local/bin/monitordiskspace.

gksudo gedit /usr/local/bin/monitordiskspace 

2. Copy the following script into the text editor:

#!/bin/sh

# This script is an adapted from Hans Zoebelein's ([email protected])
# script found at http://www.faqs.org/docs/Linux-HOWTO/Tips-HOWTO.html#ss2.3
#
# Shoot and forget: Put 'check_hdspace &' in rc.local.
# Checks for available space on devices every $SLEEPTIME sec.
# You even might check your floppies or tape drives. :)
# If available space is below $MINFREE (kb), it will echo a warning
# and send one mail for each triggering device to $MAIL_TO_ME.
# If there is more available space than trigger limit again,
# mail action is also armed again.
#

# TODO:
# Detect partitions automatically at startup
# Different $MINFREE for each device.
# Free /*tmp dirs securely from old junk stuff if no more free space etc.
# Check https://wiki.ubuntu.com/FullFilesystemSanityGutsy and
# https://wiki.ubuntu.com/HardyFullDiskHandling for the whole picture.

DEVICES='/dev/sda2'         # device; your put disks here, separated by blanks
MINFREE=20480                                   # kb; below this do warning
SLEEPTIME=10                                    # sec; sleep between checks


# ------- no changes needed below this line (hopefully :) -------

MINMB=0
ISFREE=0
MAILED=""
MINMB=`expr $MINFREE / 1024`         # yep, we are strict :)

while [ 1 ]; do
        DF="`/bin/df`"
                for DEVICE in $DEVICES ; do
                ISFREE=`echo $DF | sed s#.\*$DEVICE" "\*[0-9]\*" "\*[0-9]\*" "\*## | sed s#" ".\*##`
                if [ $ISFREE -le $MINFREE ] ; then
			ISMB=`expr $ISFREE / 1024`
                        if [ -z  "`echo $MAILED | grep -w $DEVICE`" ] ; then
                                MAILEDH="$MAILED $DEVICE"
                                MAILED=$MAILEDH
				if $( zenity --question --text "WARNING: $DEVICE only $ISMB mb free.
(Trigger is set to $MINMB mb)
Do you want to empty trash and erase downloaded software archive files?" ) 
				then
					rm -r ~/.local/share/Trash/info/*
					rm -r ~/.local/share/Trash/files/*
					gksudo apt-get clean
	                                # put further action here like cleaning
	                                # up */tmp dirs...
				fi
                        fi
                        elif [ -n  "`echo $MAILED | grep -w $DEVICE`" ] ; then
                                # Remove mailed marker if enough disk space
                                # again. So we are ready for new mailing action.
                                MAILEDH="`echo $MAILED  | sed s#$DEVICE##`"
                                MAILED=$MAILEDH
                        fi                     
                done
                sleep $SLEEPTIME
done

3. Adapt the variable "DEVICES" to include your relevant devices. It should include your root and your home partition (if differ). Use the command "mount" to get a list of your devices.

Optional: Adapt "MINFREE" to your requirements. 4. Save the file and close the text editor. 5. The script should be started automatically at each system start resp. login. Therefore we need to select System -> Preferences -> Sessions. Select "add" and write the name "monitor disk space" and the command "/usr/local/bin/monitordiskspace" while the comment can be left blank. Close the dialog with "OK".

Note: This needs to be configured for every user who should be informed in case of empty disk space. 6. Close the current session and login to Ubuntu again.

The script will be executed silently in the background. If the disk space runs full the current user is warned and prompted to empty her trash bin and erase downloded software packages.

See Also

For the big picture see the following links.