Skip to content

Check your disk space…

March 22, 2017

Here a very simple code:

df | grep -q '^/.*[09][0-9]%'

It is a simple script to check the remaining space on the disk drives of your system. Basically, I written this one because I use a VPS and the disk space is quite expensive.

Do you need an explanation for this simple line of code?

“df” is the command to get the free disk space. On a UBUNTU 16.04 (also called Xenial), it gives some lines like these ones:

Filesystem     1K-blocks     Used Available Use% Mounted on
udev             1005804        0   1005804   0% /dev
tmpfs             167060    18292    148768  11% /run
/dev/sda         9223544  6979060   1819296  80% /
tmpfs             629976        0    629976   0% /dev/shm
tmpfs               5120        0      5120   0% /run/lock
tmpfs             629976        0    629976   0% /sys/fs/cgroup
tmpfs                 24        0        24   0% /var/gandi
cgmfs                100        0       100   0% /run/cgmanager/fs
/dev/sdc        16382888 12354496   3231568  80% /srv/data_01
tmpfs             125996        0    125996   0% /run/user/1001

Only physical disks have interest. Then the regular expression will take:

  • only filesystems starting with “/” (other are special ones).
  • and a line with a percentage expressed with a number having “[09][0-9]” just before (means 100% and 9?%).

Every time the space of your disk will reach 90% or more, you will receive a message. To do so, just move the script in the /etc/cron.hourly directory (to run it every hour). If the script finds a line that matches, a mail is generated.

Note, you need to add a “.forward” file in the root directly to send the mail to your Google/Yahoo/dedicated mailbox (I suppose you’ve already done this because having your emails stuck in the local root mailbox of your Ubuntu sytem is not very useful, especially if it is a remote server). See here for details.


From → Other

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: