In my PHP web application, I am trying to limit the number of CPU/memory intensive processes that run (for example, ImageMagick's 'convert' command). I have a number of crons jobs that execute various scripts that could potentially execute too many instances of these CPU/memory intensive processes.
In my attempt to limit such processes, I first check to see if my system is already running a certain number of processes. The function:
function has_reached_process_limit($process, $limit)
{
$command = 'ps -eo comm | grep ' . $process;
exec($command, $output, $return);
if (count($output) > $limit)
{
return TRUE;
}
else
{
return FALSE;
}
}
So, I run something like this:
while (has_reached_process_limit('convert', 5) === TRUE)
{
// loop over
}
The problem is that when I monitor my OS' resources (via the 'top' command), I see a LOT more processes than what I expect to be running. Any ideas why?