I was having a problem with a large script I need to run - was a loop through about 50,000 records and downloading several pictures for a bunch of them, and updating the database.
the problem came as I started getting visitors to my site, the server would get behind, run out of memory, iowait skyrockets, mysql slows down... was a total downhill spiral.
Use this to fix it.
$load = sys_getloadavg();
$sleep=5;
$maxload=2;
if ($load[0] > $maxload) {
sleep($sleep);
echo "Busy server - sleep $sleep seconds<br>";
}
I have to play with the load and the sleep number to find what worked for my script, but now my server does not bog at all.