Checking The Number Of Running PHP Script Copies

By | May 9, 2008

Sometimes you need to run several copies of one script and need to know the number of such processes. There is a simple script that allows to easily count the number of script copies with the only limitation: dcript filename should be the same and it should be run using PHP CLI as the script uses grep tp determine running processes. Here is the code – it is quite short.

<?
// Starting to catch output buffer
ob_start();
// Running a system command to determine the number of script copies
$vase=system(“ps aux | grep script.php”);
// Getting buffer contents
$out1 = ob_get_contents();
// Cleaning output buffer (not necessary, but I’ve added it for a clean code)
ob_end_clean();
// Counting the number of strings
$ex=explode (“n”, $out1);
$suru=count($ex);
// Eching the variable with number of strings
echo $suru;
?>