如何关闭exec()函数命令或由exec()函数运行的关闭命令?

时间:2020-09-16 09:13:23

标签: php command exec wkhtmltopdf

$cmd_pdf = 'xvfb-run --wait=0******************';
exec($cmd_pdf, $output_pdf);

该命令显示“资源使用过多:”错误时,如何关闭该命令。

1 个答案:

答案 0 :(得分:0)

我遇到了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);
}