【问题标题】:How to close exec() function command or close command run by exec() function?如何关闭 exec() 函数命令或关闭 exec() 函数运行的命令?
【发布时间】:2021-01-03 02:18:07
【问题描述】:
$cmd_pdf = 'xvfb-run --wait=0******************';
exec($cmd_pdf, $output_pdf);

如何关闭此命令,因为它显示“资源使用过多:”错误。

【问题讨论】:

    标签: php command exec wkhtmltopdf


    【解决方案1】:

    我遇到了 wkhtmltopdf 无限期挂起的问题,因此我使用 timeout 实用程序运行它,它允许您设置执行时间限制。

    <?
    $htmlFilePath = 'myfile.html';
    $pdfFilePath = 'myfile.pdf';
    
    /*
     * Current version of wkhtmltopdf has some deep down nasty bug that causes it to hang
     * indefinitely at random on linux. Let's add some retry logic to work around that.
     */
    $output = array();
    $timeoutStatusCode = 124;//Timeout utility will return 124 as the exit code if it does in fact time out
    $timoutTime = '15s'; //In prod, most PDFs seem to take < 1.5 seconds, but let's be generous.
    $exitStatus = null;
    $maxIterations = 15; //I've seen nine in a row fail, mostly only takes one retry
    $currIteration = 0;
    
    $shellArgs = '--use-xserver --viewport-size 1280x1024';//...
    do
    {
        exec('export DISPLAY=":0";timeout '.$timoutTime.' wkhtmltopdf '.$shellArgs.' '.$htmlFilePath.' '.$pdfFilePath, $output, $exitStatus);
    
        $currIteration++;
    } while($exitStatus==$timeoutStatusCode && $currIteration<$maxIterations);
    
    /*
     * Make sure PDF creation was successful before post processing. Throw an exception if generation failed
     */
    if($exitStatus==$timeoutStatusCode)
    {
        throw new Exception('PDF Generation failed for '.$htmlFilePath);
    }
    

    【讨论】:

    • 嗨 Rob,我使用了超时但没有工作。 $timeoutTime = '120s'; $cmd_pdf = 'xvfb-run --wait=0 timeout '.$timoutTime.' wkhtmltopdf -B 0 -L 0 -R 0 -T 0 -q --page-height ' 。 $page_height 。 ' --page-width' 。 $page_width 。 '-s A4' 。基本名称($html_file)。 ' ' 。基本名称($pdf_file); exec($cmd_pdf, $output_pdf);
    • timeout 是一个包装了其他命令的命令,所以它必须在链中的第一个。查看它的文档。
    最近更新 更多