开发者

system($cmd) timeout in PHP

I am use url2bmp.exe to catch site screenshoot with php. my code like:

<?php
$cmd = 'url2bmp.exe -url "http://www.filmgratis.tv/index.php/category/film/animazione" -format jpeg -file"C:\www\Sc开发者_运维百科reenshot\screenshoot.jpg" -wait 5 -notinteractive run and exit when done -removesb remove right scroll bar';
system($cmd);
?>

but some time, the site page has some loading problems and the url2bmp will stop in this site and never close itself still waiting loading the page. how to use php code terminate url2bmp.exe after run in 5 seconds if it met this situation?

And another question, the site will pop-up ads in a new ie windows, how to stop open a new ie windows with php? Thanks.


You can't set a timeout, but you can monitor the process and kill it if it passes the 5 second timeout. Here's some code (from here) on Windows (see here for Linux). $command is the command to execute, $timeout is how long to let the process run for (5 seconds in your case) and $sleep is the interval between timeout checks (1 second should be suitable for your case).

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; 

       echo "\n ---- $cur ------ \n"; 

        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($commandJob) { 

    $command = $commandJob.' > /dev/null 2>&1 & echo $!'; 
    exec($command ,$op); 
    $pid = (int)$op[0]; 

    if($pid!="") return $pid; 

    return false; 
} 

function PsExists($pid) { 

    exec("ps ax | grep $pid 2>&1", $output); 

    while( list(,$row) = each($output) ) { 

            $row_array = explode(" ", $row); 
            $check_pid = $row_array[0]; 

            if($pid == $check_pid) { 
                    return true; 
            } 

    } 

    return false; 
} 

function PsKill($pid) { 
    exec("kill -9 $pid", $output); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜