Skip to content

Instantly share code, notes, and snippets.

@jhead
Created April 16, 2014 18:29
Show Gist options
  • Save jhead/10917466 to your computer and use it in GitHub Desktop.
Save jhead/10917466 to your computer and use it in GitHub Desktop.
<?php
$procList = array();
$newProcList = array();
$limitProcs = array();
while (true) {
$count = 0;
$newProcList = getProc();
foreach ($newProcList as $pid) {
if (!in_array($pid, $procList)) {
$limitProcs[$pid] = limitProc($pid);
$procList[] = $pid;
$count++;
}
}
echo "Added $count \\ ";
$count = 0;
foreach ($procList as $k => $pid) {
if (!in_array($pid, $newProcList)) {
unlimitProc($limitProcs[$pid]);
unset($limitProcs[$pid]);
unset($procList[$k]);
$count++;
}
}
echo "dropped $count processes. ";
echo "Waiting...";
sleep(3);
echo "\n";
}
foreach (getProc() as $pid) { echo limitProc($pid) . "\n"; }
function getProc() {
$cmd = "echo $(ps acx | grep java | awk '{ print $1 }')";
$proc = explode(" ", shell_exec($cmd));
foreach ($proc as $k => $v) $proc[$k] = intval($v);
return $proc;
}
function limitProc($pid) {
$limit = 75;
$limit_pid = intval(shell_exec("cpulimit -p $pid -l $limit &> /dev/null & echo $!"));
return $limit_pid;
}
function unlimitProc($pid) {
shell_exec("kill -9 $pid > /dev/null 2>&1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment