Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Pause and resume resource-intensive apps via script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Pause and resume resource-intensive apps via script
Authored by: SOX on May 17, '06 08:50:12AM
Here's an annotated verison of the script that could be saved to a file instead of being an command line alias.

#!/usr/bin/perl -w
# expects 3 args: job#  high_load_trip  Low_load_trip

die unless @ARGV;    # exit if no args on command line

# install a signal handler to assure the watched job is left running if the user breaks out of this script.
$SIG{INT} =sub {
              `kill -s CONT $p`; 
              die  qq:bye now\n:
              };

# get the args.  use default args for trip value if there is only one arg on command line
($p,$s,$c) = ( @ARGV,2.5,0.5);

# initialize state and wait period to "continue" and 30 seconds
($sig,$w) = (qq:CONT:,30);

# initialize the counters
$c1=$c2=$e1=$e2=0;$e2=30;

# main loop.
while (1) { 
       # see if wathced process is still in the process list and
       # gracefully exit if it it not there any more.
       last unless  grep{ /^\s*$p /} `ps x`;

      # read and parse the load averages for last 1 an ten minutes
      ($x,$y,$z) = split /\s+/,`sysctl vm.loadavg`; 

      #  if the 1 minute load average ($y)  is above the high_trip ($s) 
      # then set the state to STOP and the wait increment to 300 seconds
      ($sig,$w,$e1,$e2)=(qq:STOP:,300,300,0) if ($y>$s) ;

      # on the other hand if the 10-minute load average is less then the low_trip
      # the set the state to CONT and the wait increment to 30 seconds
      ($sig,$w,$e1,$e2) =(qq:CONT:,30,0,30) if ($z<$c);

      # otherwise just continue doing what ever you did last

       # send the signal to CONT or STOP to the job
       `kill -s $sig $p` ;

       # display state on terminal and snooze
       print STDERR qq/ $p: $x $y $z   $sig  S:$c1 C:$c2 sec                /,chr(13); 
       sleep $w;  

       # increment counters.
       $c1+=$e1;$c2+=$e2
}


[ Reply to This | # ]