Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my PHP script tell whether the server is busy?

Tags:

php

mysql

load

I want to run a cron job that does cleanup that takes a lot of CPU and Mysql resources. I want it to run only if the server is not relatively busy.

What is the simplest way to determine that from PHP? (for example, is there a query that returns how many queries were done in the last minute? ...)

like image 670
Nir Avatar asked Mar 10 '09 20:03

Nir


1 Answers

if (function_exists('sys_getloadavg')) {
    $load = sys_getloadavg();
    if ($load[0] > 80) {
       header('HTTP/1.1 503 Too busy, try again later');
       die('Server too busy. Please try again later.');
    }
}

Try to update this function to your needs

like image 172
SMka Avatar answered Oct 25 '22 20:10

SMka