note 59428 added to function.exec
| From: | period) | Date: | Tue, 06 Dec 2005 20:51:03 +0000 |
| Subject: | note 59428 added to function.exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99647@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.exec.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59428
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59428&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59428&report=yes
Search -- http://master.php.net/manage/user-notes.php