note 59428 deleted from function.exec by cmb

From: Date: Sun, 24 Nov 2019 08:42:16 +0000
Subject: note 59428 deleted from function.exec by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213199@lists.php.net to get a copy of this message
Note Submitter: amandato (at) gmail (period) com ---- Running a command that may never time out on a windows server inspired the following code. The PsExecute() function allows you to run a command and make it time out after a set period of time. The sleep parameter is the number of seconds the script will pause becore checking if the process is complete. Code utilizes the PsTools, found here: http://www.sysinternals.com/ntw2k/freeware/pstools.shtml Copy and paste the following code into an include file: <?php // pstools.inc.php function PsExecute($command, $timeout = 60, $sleep = 2) { // First, execute the process, get the process ID $pid = PsExec($command); if( $pid === false ) return false; $cur = 0; // Second, loop for $timeout seconds checking if process is running while( $cur < $timeout ) { sleep($sleep); $cur += $sleep; // If process is no longer running, return true; if( !PsExists($pid) ) return true; // Process must have exited, success! } // If process is still running after timeout, kill the process and return false PsKill($pid); return false; } function PsExec($command) { exec( dirname(__FILE__). "\\psexec.exe -s -d $command 2>&1", $output); while( list(,$row) = each($output) ) { $found = stripos($row, 'with process ID '); if( $found ) return substr($row, $found, strlen($row)-$found-strlen('with process ID ')-1); // chop off last character '.' from line } return false; } function PsExists($pid) { exec( dirname(__FILE__). "\\pslist.exe $pid 2>&1", $output); while( list(,$row) = each($output) ) { $found = stristr($row, "process $pid was not found"); if( $found !== false ) return false; } return true; } function PsKill($pid) { exec( dirname(__FILE__). "\\pskill.exe $pid", $output); } ?> Place the above include file and the PsTools from (http://www.sysinternals.com/ntw2k/freeware/pstools.shtml) in the same directory.

« previous php.notes (#213199) next »