1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pause a php scirpt if CPU gets too high

Discussion in 'PHP & Perl' started by traxxas, May 10, 2013.

  1. traxxas

    traxxas Newbie

    Joined:
    Nov 1, 2011
    Messages:
    29
    Likes Received:
    5
    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
     
  2. divok

    divok Senior Member

    Joined:
    Jul 21, 2010
    Messages:
    1,020
    Likes Received:
    636
    Location:
    http://twitter.com/divok
    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
     
  3. babasss

    babasss Regular Member

    Joined:
    Jul 12, 2010
    Messages:
    335
    Likes Received:
    197
    or just buy a higher cpu (at least with 6 cores)
     
  4. healzer

    healzer Jr. Executive VIP Jr. VIP Premium Member

    Joined:
    Jun 26, 2011
    Messages:
    2,367
    Likes Received:
    1,970
    Gender:
    Male
    Occupation:
    Marketing automation tools
    Location:
    Somewhere in Europe
    Home Page:
    or adjust the core of your script.
    make it run slowly but continuously
     
  5. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,310
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
    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);
     
    • Thanks Thanks x 5
  6. traxxas

    traxxas Newbie

    Joined:
    Nov 1, 2011
    Messages:
    29
    Likes Received:
    5
    Thanks will try this :)
     
  7. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,114
    Likes Received:
    2,892
    Gender:
    Male
    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.
     
  8. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,114
    Likes Received:
    2,892
    Gender:
    Male
    That's smart!