Warning: Declaration of ThemeSwitcherWidget::update($new_instance) should be compatible with WP_Widget::update($new_instance, $old_instance) in /home/kingston/hitormiss/wp-content/plugins/theme-switcher/theme-switcher.php on line 0
Monitor Disk Space PHP Script

Hit or Miss

Monitor Disk Space PHP Script

Problem: The /var paritition on my linux box has a habit of rapidly filling up, causing my server to crash.
I needed some way to automatically alert myself when the free space on the partition fell below a certain level.

Solution: I wrote a simple script in PHP that sends a text message to my pager if the space used in the /var partition exceeds 12%.


    $output = array();
    exec("df", $output);
    $output = join("n", $output);
    $space = eregi_replace(".* ([0-9]+)% /var.*", "1", $output);

    if ($space > 12)

      $pin = "2128232";
      $msg = "/var at $space%";
      $msg = urlencode($msg);
      $url = "http://www.radiocommpaging.com/".

      $fp = @fopen ($url, "r");



The script first loads the results of the df command into an array. The output from that command on my linux box looks something like this:

Filesystem 1k-blocks    Used Available Use% Mounted on
/dev/sda5  1517920    259200   1181612  18% /
/dev/sda1    23302      8976     13123  41% /boot
/dev/sda9  2213668    553376   1547840  27% /home
/dev/sda7  1517920    736560    704252  52% /usr
/dev/sda8   806368     43432    721972   6% /var
/dev/sdb4    98078     66026     32052  68% /mnt/zip

Then, I parse through the output array to look for the size of the /var parition using the eregi_replace command.

If your pager can receive text messages, there are generally two ways of sending a message to it. Some pagers have actual email addresses, so you can send messages to them using the mail function. My pager only recieves messages sent from a form on the company’s website, so I submit variables to the form processing page using the fopen function.

Now the only thing left to do is set this program to run every 30 minutes. Use the crontab command at the unix prompt to schedule this script to run automatically. The entry in my cron file looks something like this…

   0,30 * * * * php /path/to/monitor_disk_space.php

All in all, it’s pretty simple and rudimentary, but it works for me. Your mileage might vary.

8 responses so far (Respond)


Hey thanks for this awesome script. I used it to learn how to get the free space from the “df” command for my own project. Heres the code I used:

$output = array();
exec(“df -h | grep /dev/hda2 | cut -b41-44”, $output);
$output = trim(join(“n”, $output));
echo $output;

I wish my website had such a cool domain name… 😛

Daniel | 3 Jan 2006

I expanded on what Daniel wrote so that I could execute shell commands from the web browser and see the output

“, $output));
echo $output;


Me | 3 Jun 2006

Last comment didn’t post the code, simply put a text box called shellcmd and a submit button on a page with the below php code.

$output = array();
exec($_REQUEST[‘shellcmd’], $output);
$output = trim(join(“”, $output));
echo $output;

Me | 3 Jun 2006

All I gett from this snippet:

$output = array();
exec(”df -h | grep /dev/hda2 | cut -b41-44″, $output);
$output = trim(join(”n”, $output));
echo $output;


Warning: Division by zero in /var/www/sitetrafic/test.php on line 4

Warning: Division by zero in /var/www/sitetrafic/test.php on line 4

What’s wrong??????

Henke | 9 Nov 2006

oo so we can run all shell commands from PHP?

Bidfreelancers | 29 May 2007

This is NOT correct:
$space = eregi_replace(“.* ([0-9]+)% /var.*”, “1″, $output);

This works ok:
$space = eregi_replace(“.* ([0-9]+)% /var.*”, “\1″, $output);

Please change: “1” -> “\1”

Ruben | 4 Dec 2008

When you copy the original code, remove all of the funny curved double quotes (“″) and replaced them with the standard straight double quote (“).

Then your script should run fine.

Keith Spiller | 26 May 2009

# Get Drive Capacity for Linux use "/" instead of "C:"
$capacity = number_format(disk_total_space("C:")/1024/1024/1024,2);
# Get Free Space on Drive – for Linux use "/" instead of "C:"
$freespace = number_format(diskfreespace("C:")/1024/1024/1024,2);

# note the above calculation shows the space in GB!
# if you want to be emailed if the space is less than…. eg. 8GB
if ($freespace

Arun Devarajan | 13 May 2011