PHP 中的系统($cmd)超时

发布于 2024-10-12 05:28:29 字数 480 浏览 3 评论 0原文

我使用 url2bmp.exe 用 php 捕获网站屏幕截图。我的代码如下:

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

但有时,网站页面存在一些加载问题,并且 url2bmp 将停止在该网站中,并且永远不会自行关闭,仍在等待加载页面。如果遇到这种情况,如何使用php代码在运行5秒后终止url2bmp.exe?

还有一个问题,网站会在新的ie窗口中弹出广告,如何用php阻止打开新的ie窗口?谢谢。

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\Screenshot\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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2024-10-19 05:28:29

您无法设置超时,但您可以监视进程并在超过 5 秒超时后将其终止。以下是 Windows 上的一些代码(来自此处)(请参阅此处(适用于 Linux)。 $command 是要执行的命令,$timeout 是让进程运行的时间(在您的情况下为 5 秒),$sleep是超时检查之间的间隔(1 秒应该适合您的情况)。

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); 
}

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); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文