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:
#!/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 85 ]; then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep%" -a "From: server@codingstories.net" admin_email@gmail.com fi done
How you use the script is simple:
- Change the “server@codingstories.net” to your server from address
- Change the “admin_email@gmail.com” 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.
Bonus:
Cleaning autoptimize cache when your server starts running out of space:
#!/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 85 ]; then rm -rf /home/yourwww/wp-content/cache/autoptimize/* echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep%" -a "From: server@codingstories.net" admin_email@gmail.com fi fi done
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%!
Leave a Reply