note 10332 deleted from usleep by ronabop

From: Date: Wed, 03 Jan 2001 07:38:42 +0000
Subject: note 10332 deleted from usleep by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-9319@lists.php.net to get a copy of this message
To monitor a scripts CPU ussage and avoid any nasty CPU gobbling loops you can use this function (will not work with windows or safe mode) I know it works on FreeBSD function phpmon($max) { $usage = $max +1; $sleeprate = 500; $loops = 1; while ($usage >= $max) { $cmd = ps -Unobody -r -o%cpu; $lines = explode("n", $cmd); $usage = substr($lines[1], 0, strpos($lines[1], ".")); $sleeprate = $loops + $sleeprate; usleep($sleeprate); $loops+=100; } } just place this in your potential problem loop: phpmon($MAX); where $MAX is the maximum CPU you want the process to consume. e-mail me with any improvements/suggestions.

« previous php.notes (#9319) next »