11

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

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

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

    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;
   }
Brad
  • 159,648
  • 54
  • 349
  • 530
robert
  • 143
  • 1
  • 3
  • 10
  • 1
    Btw, this has been answered at least three times: http://stackoverflow.com/questions/1176497/limit-execution-time-of-an-function-or-command-php, http://stackoverflow.com/questions/1928249/php-preventing-max-execution-time-limit-for-mail and http://stackoverflow.com/questions/2147567/avoid-php-execution-time-limit – Gordon Feb 04 '10 at 19:10

3 Answers3

21

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

set_time_limit($seconds)

If $second is 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. 180 is better than no limit.

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);
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • ok i put set_time_limit(180) before exec($command); It seems to work! but after the time expires i want to echo a error how i do this?? – robert Feb 04 '10 at 19:25
  • It will throw an error by itself when the time limit expires. That's what made you ask your question in the first place. If you want a custom error handler, see http://php.net/manual/en/function.set-error-handler.php – Gordon Feb 04 '10 at 19:33
  • yes, but when i submit a form a process.php page start with a iframe, i only whant to echo this: where the result is a error or succes! – robert Feb 04 '10 at 21:47
  • If `set_error_hander` doesn't help, then I'm afraid I don't understand. Consider opening a new question, as it is a different problem. – Gordon Feb 04 '10 at 21:55
  • I hope `set_time_limit(0)` overrides the maximum execution time for the current call? – Suhail Gupta May 11 '16 at 08:12
  • 1
    @SuhailGupta for the entire *request*. – Gordon May 11 '16 at 10:48
4

In .htaccess you can set these values:

php_value max_execution_time 1000000
php_value max_input_time 1000000

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

set_time_limit(0);
campsjos
  • 1,275
  • 15
  • 22
John Conde
  • 217,595
  • 99
  • 455
  • 496
3

you can use ini_set() like this:

ini_set('max_execution_time', 0);

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

soulmerge
  • 73,842
  • 19
  • 118
  • 155