Fatal error: Maximum execution time of 30 seconds exceeded
Asked Answered
C

18

471

I am downloading a JSON file from an online source and and when it runs through the loop I am getting this error:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\temp\fetch.php on line 24

Colt answered 2/3, 2011 at 8:11 Comment(1)
"The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows" From PHP DocumentationPromotion
R
821

Optimize your code

You need to optimize your code and look for errors.

For example, you could mistakenly create an endless loop. Obviously, in this case increasing the execution time won't help.

Or, given you are storing the received data in the database, there are techniques that can greatly improve the performance, such as using transactions or multi-insert queries.

Profile your code, find performance bottlenecks and optimize them.

Run as CLI

In case the code is already optimized but inevitably takes too much time, consider execute it not as a web-page call but as a command line script. When called from a command line, PHP scripts aren't affected by the time limit.

For example, you can configure this JSON download as a cron job, or implement some queue, when your web-page only creates a job, that takes a fraction of second, while some background process will take time to download, parse and store your JSON.

Increase the time limit

As a last resort you could temporarily extend the time limit, using either

ini_set('max_execution_time', '300'); //300 seconds = 5 minutes

or

set_time_limit(300);
Rayraya answered 2/3, 2011 at 8:14 Comment(12)
set_time_limit(300) can be used to temporarily extend the time limit.Dagnah
Is this set only for that one script or will it affect all other scripts executed after this one on the same server?Nicks
@Rao yes, this will be only for that one script. To make it for all scripts you need to change it in the php.ini file. you could also do set_time_limit(0); if you want the script to run forever, or for a long timeGibberish
ini_set('max_execution_time', 0); //no limitMcgrew
Is this good practice? For example, I have already placed order in my mysql data table and email is being sent to customer. This email sending task keeps on giving maximum execution timeout error. My question is again: extending time or catching the exception is good? Help please.Theophylline
@sangam I'd say it's best practice, to keep this limit as low as possible while allowing a certain amount of extra buffer. Out of security reasons I would not want to turn it off completely. It should be set reasonable. I actually set it to twice the amount of time my longest running script takes for completion under a medium server-stress-level. Still it could always fail. So best practice is also to deal with time-out issues for time consuming scripts separately.Cream
Use ini_set('max_execution_time', '300'); when using strict typing (<?php declare(strict_types=1);)Gilder
And do not forget to restart the server )Vikki
hmm, both are identical.Algeria
@GamerInTheGame Why?Nike
@Nike because ini file changes will be applied after restartFable
@YourCommonSense set_time_limit(300); is not a change to the ini file, hence my questionNike
B
212

I had the same problem and solved it by changing the value for the param max_execution_time in php.ini, like this:

max_execution_time = 360      ; Maximum execution time of each script, in seconds (I CHANGED THIS VALUE)
max_input_time = 120          ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M           ; Maximum amount of memory a script may consume (128MB by default)

I hope this could help you.

Beora answered 12/4, 2012 at 17:2 Comment(5)
For some reason ini_set isn't working but that helps for sure!Wulfe
php.ini can be found in /etc/php5/apache2/php.ini.... I'm using ubuntu 13.04...Prothorax
I had same problem when installing moodle on wamp but solved configuring php.ini fileArchitect
Need to restart apache after the change in php.iniTalkington
[php7] - sudo nano /etc/php/7.0/apache2/php.iniBobettebobina
A
57

All the answers above are correct, but I use a simple way to avoid it in some cases.

Just put this command in the begining of your script:

set_time_limit(0);
Aerate answered 24/1, 2014 at 19:14 Comment(3)
This is not recommend at allGainsay
Turned off time limit does not protect against infinite loops, so always set up a (greater) limit.Nob
@IamtheMostStupidPerson Why not? If you use it as localhostShandy
C
26

I ran into this problem while upgrading to WordPress 4.0. By default WordPress limits the maximum execution time to 30 seconds.

Add the following code to your .htaccess file on your root directory of your WordPress Installation to over-ride the default.

php_value max_execution_time 300  //where 300 = 300 seconds = 5 minutes
Cyme answered 7/9, 2014 at 2:25 Comment(1)
see wpbeginner.com/wp-tutorials/…Pash
E
18

Edit php.ini

Find this line:

max_execution_time

Change its value to 300:

max_execution_time = 300

300 means 5 minutes of execution time for the http request.

Expensive answered 25/1, 2014 at 16:15 Comment(0)
S
16

Your script is timing out. Take a look at the set_time_limit() function to up the execution time. Or profile the script to make it run faster :)

Sexagenary answered 2/3, 2011 at 8:15 Comment(1)
@HarshaMV He means use a performance profile to determine why the script is slow to begin with, and optimize your code to improve execution time. This may or may not be useful, depending on what the bottleneck is (CPU vs. Disk I/O vs. Network).Stupa
C
13

if all the above didn't work for you then add an .htaccess file to the directory where your script is located and put this inside

<IfModule mod_php5.c>
php_value post_max_size 200M
php_value upload_max_filesize 200M
php_value memory_limit 300M
php_value max_execution_time 259200
php_value max_input_time 259200
php_value session.gc_maxlifetime 1200
</IfModule>

this was the way I solved my problem , neither ini_set('max_execution_time', 86400); nor set_time_limit(86400) solved my problem , but the .htaccess method did.

Catholicon answered 9/7, 2015 at 13:25 Comment(2)
It works in my case. Thank you. Replace M with G for GBUnheard
If running on php 7 just change from mod_php5 to mod_php7Venus
R
10

We can solve this problem in 3 different ways.

1) Using php.ini file

2) Using .htaccess file

3) Using Wp-config.php file ( for Wordpress )

Rogovy answered 29/1, 2015 at 9:27 Comment(1)
Showing examples of the 3 ways to solve the problem would be most helpful.Fernandina
M
9

You can remove the restriction by seting it to zero by adding this line at the top of your script:

<?php ini_set('max_execution_time', '0'); ?>
Macklin answered 22/3, 2017 at 6:40 Comment(0)
J
8

To extend your max_execution_time you can use either ini_set or set_time_limit.

// Set maximum execution time to 10 seconds this way
ini_set('max_execution_time', 10);
// or this way
set_time_limit(10);

!! But be aware that, both functions restarts also counting of time script has already taken to execute

sleep(2);
ini_set('max_execution_time', 5);

register_shutdown_function(function(){
    var_dump(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
});

for(;;);

//
// var_dump outputs float(7.1981489658356)
//

so if you want to set exact maximum amount of time script can run, your command must be very first.

Differences between those two functions are

  • set_time_limit does not return info whether it was successful but it will throw a warning on error.
  • ini_set returns old value on success, or false on failure without any warning/error
Jaramillo answered 27/5, 2016 at 13:35 Comment(0)
P
7

Maybe check for any thing that you have changed under the php.ini file. For example I changed the ";intl.default_locale =" to ";intl.default_locale = en_utf8" in order to enable the "Internationalization extension (Intl)" without adding the "extension=php_intl.dll" then this same error occurred. So I suggest to check for similar mistakes.

Periphrasis answered 24/6, 2013 at 18:35 Comment(0)
J
7

Follow the path /etc/php5(your php version)/apache2/php.ini.

Open it and set the value of max_execution_time to a desired one.

Jaclin answered 6/3, 2014 at 12:53 Comment(1)
On certain hosting providers you might need to set on /home/[username]/.php/7.0/phprcSpinner
H
6

You can do it easily with WHM. Just got to:

WHM -> Service Configuration -> PHP configuration

editor-> max_execution_time=30

( 30 is default change it to whatever value u want)

Hurry answered 27/10, 2012 at 3:7 Comment(0)
T
6

Increase your script execution time by adding the following line at top of the PHP script.

ini_set('max_execution_time', 120); //120 seconds = 2 minutes

Reference has taken from Increase the PHP Script Execution Time

Trilby answered 22/11, 2015 at 18:39 Comment(0)
A
3

I have same problem in WordPress site, I added in .htaccess file then working fine for me.

php_value max_execution_time 6000000
Aspinwall answered 18/12, 2015 at 9:56 Comment(0)
C
3
set_time_limit($time_in_second); // 0 for unlimited time

I use this php function in run time if need to run a script for long. Details here.

Closefisted answered 29/4, 2022 at 9:58 Comment(0)
F
0

I have make some changes in my case in: xampp\phpMyAdmin\libraries\config.default.php

i have searched for : $cfg['ExecTimeLimit'] and change the value at right side...

You can change Right hand Value to any higher value, like '5000'. and it works.

Fraction answered 30/3, 2019 at 11:29 Comment(0)
C
0

in my case : nano /etc/php/7.4/fpm/php.ini

set this : max_execution_time = 300

Solve my issue.

Increase php exécution time

Chairborne answered 26/12, 2023 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.