How to get notified when your server HDD gets low

Ever since the PageSpeed hype started I’m starting to see this more and more. In order to cover all Google’s terms for high PageSpeed people start employing more and more tools like Autoptimize to inline css, minify JS and so on. However if you run Autoptimize on a WPMU installation there is a case in which it can generate so much cached files that at the end it will eat all of the server’s memory and leave the database stranded. I’ve seen it cause “Error establishing a database connection”, sending developers and system administrators looking in a completely different direction. I’ve seen it cause Apache to completely stop. Weird things happen to servers when they’re deprived of buffer storage.
So in order to make sure our servers always have at least 85% HDD I came up with the following bash script:

How you use the script is simple:

  • Change the “” to your server from address
  • Change the “” to your email address
  • Change “85” to the percentage you want to be notified at
  • Save the script to a file, for example hdd_notify and copy it to /usr/local/bin
  • Run “sudo chmod +x /usr/local/bin/hdd_notify”

Now run “crontab -e” and setup your script to run as frequently as you want. You can use our Crontab Generator to configure your cron.


Cleaning autoptimize cache when your server starts running out of space:

Now you have to change “/home/yourwww” to your actual website root, and be very very careful with that path, because this script will irreversibly delete everything in that folder after your server HDD runs below 85%!

About Pavel Petrov 2 Articles |  17 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.