Pause a php scirpt if CPU gets too high

traxxas

Newbie
Joined
Nov 1, 2011
Messages
43
Reaction score
42
Looking for a way to make my php cron job to sleep if the CPU use gets too high lets say it sleeps for 30sec - 1 min and checks the CPU and if its still too high it sleeps for another minute or so and keeps doing that until the CPU use had gone down

Dose anyone have any idea how to do this without breaking the script?


Thanks
 
Code:
[FONT=Arial, Liberation Sans, DejaVu Sans, sans-serif][COLOR=#4a6b82]http://phpsysinfo.sourceforge.net/[/COLOR][/FONT]
it can be used to check Cpu etc , tough you will have to modify it to your needs
 
or just buy a higher cpu (at least with 6 cores)
 
or adjust the core of your script.
make it run slowly but continuously
 
Code:
function get_server_load()
{
$tmp=file_get_contents('/proc/loadavg');
$temp=explode(' ',$tmp);
return $temp[0];
}

function check_cpu_and_sleep($val=0,$sleep=0)
{
if(get_server_load()>$val)
  sleep($sleep);
}

in your script add in a loop or whereever you need like this:

check_cpu_and_sleep(LOAD_HERE,SECONDS_TO_SLEEP);
 
I don't think you can do it directly through PHP.Considering that you have a Nix server, you need a shell script that would check the resources and run the script according to that.
Looking for a way to make my php cron job to sleep if the CPU use gets too high lets say it sleeps for 30sec - 1 min and checks the CPU and if its still too high it sleeps for another minute or so and keeps doing that until the CPU use had gone down

Dose anyone have any idea how to do this without breaking the script?


Thanks
 
That's smart!
Code:
function get_server_load()
{
$tmp=file_get_contents('/proc/loadavg');
$temp=explode(' ',$tmp);
return $temp[0];
}

function check_cpu_and_sleep($val=0,$sleep=0)
{
if(get_server_load()>$val)
  sleep($sleep);
}

in your script add in a loop or whereever you need like this:

check_cpu_and_sleep(LOAD_HERE,SECONDS_TO_SLEEP);
 
Back
Top