php 专门为 exec() 设置最大执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2202355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:29:19  来源:igfitidea点击:

Set maximum execution time for exec() specifically

phpexec

提问by robert

Is it possible to set maximum execution time of exec($command)function? Sometimes execution of my $commandlasts too long stopping after 1 minute and presenting this error:

是否可以设置exec($command)函数的最大执行时间?有时我的执行$command时间过长,在 1 分钟后停止并出现此错误:

Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\files.php on line 51

致命错误:第 51 行的 C:\xampp\htdocs\files.php 中超出了 60 秒的最大执行时间

How can I increase the exec()command maximum execution time?

如何增加exec()命令最大执行时间?

    if (allow()) {
    exec($command);

    if (file_exists($file)) {
        //exec($makeflv);
        echo '<script language="javascript" type="text/javascript">window.top.window.aviout(1);</script>';

    } else {
        echo $error;
        }

} else {
   echo $error;
   }

回答by Gordon

See http://php.net/manual/en/function.set-time-limit.php

http://php.net/manual/en/function.set-time-limit.php

set_time_limit($seconds)

If $secondis set to 0, no time limit is imposed. Note that you should be cautious with removing the time limit altogether. You don't want any infinite loops slowly eating up all server resources. A high limit, e.g. 180is better than no limit.

如果$second设置为0,则不施加时间限制。请注意,在完全取消时间限制时应谨慎。您不希望任何无限循环慢慢耗尽所有服务器资源。上限,例如180总比没有限制好。

Alternatively, you can adjust the time setting just for certain blocks of code and resetting the time limit after the time critical code has run, e.g.

或者,您可以仅为某些代码块调整时间设置,并在时间关键代码运行后重置时间限制,例如

$default = ini_get('max_execution_time');
set_time_limit(1000);
... some long running code here ...
set_time_limit($default);

回答by John Conde

In .htaccessyou can set these values:

.htaccess你可以设置这些值:

php_value max_execution_time 1000000
php_value max_input_time 1000000

You can also try this at the top of your PHP script:

您也可以在 PHP 脚本的顶部尝试此操作:

set_time_limit(0);

回答by soulmerge

you can use ini_set()like this:

你可以这样使用ini_set()

ini_set('max_execution_time', 0);

Setting it to 0 will make your script run forever according to the documentation of the ini-directive.

根据ini-directive文档,将其设置为 0 将使您的脚本永远运行。