Linux (cPanel) Server Daily Disk Usage And Large Directory Report

JeffTechnical Articles & Notes

Our servers automatically warn by PushBullet, Email and SMS whenever disk space hits some threshold warning level, which is handy. If you’re reading this you’re probably aware that when a server’s primary disk becomes completely full, the server becomes inoperable. A situation best avoided!

An issue though is that this alert can come at any time – and if disk space is being hogged extremely rapidly (and it can be) then there may be little time before it’s too late.

So an early warning system would be a good idea.

It’s also handy to keep an eye on the larger directories (folders) on a server and see if any are growing in an unexpected way.

A couple of commands on the command line can give you this information very easily:

This command will give you a human readable overview of your disks and their usage.

df -h

This command will give you an ordered list of the 50 largest directories on your server, and their size (excluding cPanel ‘virtfs’ directories which are symlinks to other directories). It can take a little time to run, depending upon the content of your disk:

du --max-depth=5 --exclude=/home/virtfs -x / | sort -n | tail -n 50

This is all good stuff but having to log in to each server and run these on a regular basis can become a bit of a pain. So why not have them run automatically, once a day, and have the output emailed to you?

This is easy to achieve with a ‘cron’ task as follows…

Create a script and put it somewhere appropriate on your server, with permissions 700. Call it something like ‘my_disk_check_script.sh’ and put this in it:

#!/bin/bash
df -h

du --max-depth=5 --exclude=/home/virtfs -x / | sort -n | tail -n 50

And then find and edit (on cPanel servers) this file:

/etc/crontab

Ensure you’ve got your email address set at the top of the file on this line which should be in the crontab file:

MAILTO=YOU@YOUR EMAIL DOT COM

And then add these lines at the bottom of the file:

# Daily task to tell me about disk space usage
00 06 * * * root sh /your_script_path/my_disk_check_script.sh

That’s it. Every morning at 0600 this should now email you a summary of disk usage on your server.