Just wandering how the max_execution_time works.
The documentation here states that the option sets:
the maximum time in seconds a script is allowed to run
How does this work for includes/requires?
Example:
file1.php
<?php
include("file2.php");
include("file3.php");
?>
file2.php
<?php
//some script that takes 6 minutes
?>
file3.php
<?php
echo "hello"
?>
If file2.php takes 6 minutes (and the max_execution_time is set to 5 minutes), does control pass back to file1.php and continue running, or does the whole file quit?
The execution breaks and throw a error.
Max execution time is the time your script can be executed. No matter how many includes do you have. So if if at any point of time the script will run out of time, everything will stop and you will receive
Fatal error: Maximum execution time of XX seconds exceeded
Related
currently I implement the sleep() in my PHP script to pause the execution for 15 minutes to give some buffer for other tasks to finish before continue to execute the script.
<?php
------do something here-----
file_put_contents("logs/pre_sleep.log", "$id sleep in $datetime", FILE_APPEND);
sleep(900);
----continue do something here----
file_put_contents("logs/done.log", "$id done in $datetime", FILE_APPEND);
?>
When the PHP is called from client, the process will be logged in "pre_sleep.log" and before the script finish will be logged in "done.log". However, based on the log I write, I notice the some execution will not continue even has passed for 15 minutes. The process has been logged in "pre_sleep.log" where "done.log" couldn't find the same process.
Is it possible the process been killed by others within 15 minutes? This happens seem like very randomly, because most of the processes logged in both log files but some appear in only one.
It is possible the process gets shut down after the sleep (or even before the sleep).
My best guess is, your PHP timeout is in play. Put the following at the top of your script file.
set_time_limit(0);
// or
ini_set('max_execution_time', 0);
It will allow for scripts to run forever
I have a PHP script that uses an API. Sometimes the API can be fast, sometimes it’s slow. Is there a way to check if execution time of the get_contents(); is above ex. 2 seconds?
If it goes over this amount i want the included script to stop, and execute the original file.
Ex:
<?php
include("file.php"); //if more than 2 sec, continue
echo "Hello world";
?>
You may use set_time_limit function to set the maximum time limit
.... //some logic
const MAX_EXECUTION_TIME = 2; // for 2 seconds
set_time_limit(self::MAX_EXECUTION_TIME);
file_get_contents(...);
.... // some other logic
I have this PHP code:
<?php
include_once("connect_to_mysql.php");
$max=300;
while($max--)
{
sleep(1);
doMyThings();
}
?>
it is supposed to repeat a mysql query 300 times with gap of 1 second between each. But the problem is after a minute or so in the browser i get this message: No Data Received. Unable to load the webpage because the server sent no data.
The problem is the following: Your code will at least (without considering the amount of time needed by doMyThings()) last 300 seconds. Most PHP environments set the default script running time to about 60 secs, the script stops and nothing is printed out.
Next thing is (if script execution time is set high enough to allow long running scripts), the script has to run until its finished (that is, ~300 secs) and after that, data is written onto the output stream. Until there, you won't see any output.
To circumvent those two problems, see this code:
<?php
// If allowed, unlimited script execution time
set_time_limit(0);
// End output buffering
ob_end_flush();
include_once("connect_to_mysql.php");
$max=300;
// End output buffering IE and Safari Workaround
// They will only display the webpage if it's completely loaded or
// at least 5000 bytes have been "printed".
for($i=0;$i<5000;$i++)
{
echo ' ';
}
while($max > 0)
{
sleep(1);
doMyThings();
$max--;
// Manual output buffering
ob_flush();
flush();
}
?>
Maybe this post is also of interest to you: Outputting exec() ping result progressively
The browser will not wait a whole 5 minutes for you to complete your queries.
You need to find a different solution. Consider executing the PHP script in CLI.
It seems that you have a timeout executing 300 times doMyThings();
You can try with set_time_limit(0);
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
When you execute long time php code on server side, you need change max_execution_time directive in php.ini. But browser will not wait how long as you want so you need use async technology like AJAX
I have a PHP script in my Code Igniter application,
its run on server and fetch some data but its not running more than 2 minutes approx..
and when I run this without using code igniter its works properly..what may be the reason behind this?
thanks #air4x its works . by setting set_time_limit(300) in the system/core/CodeIgniter.php
if (function_exists("set_time_limit") == TRUE AND #ini_get("safe_mode") == 0)
{
#set_time_limit(300);
}
after setting this code script running well..
Try adding this before you run your code: set_time_limit(0);
More info: http://php.net/manual/en/function.set-time-limit.php
If that doesn't work, you'll need to share what code you are running and what happens when it stops running.
There must be, Set_time_limit - Sets the maximum execution time of a script.
bool set_time_limit ( int $seconds )
When set_time_limit() called. It returns the counter to zero. In other words, if the default limit is 30 seconds, and after 25 seconds of script execution the set_time_limit(20) call is made, then the script will run for a total of 45 seconds before finishing .
Please visit http://php.net/manual/fr/function.set-time-limit.php for more information.
Is it possible to get Max Execution timeout notification so that i can use code clean up operations before the php script stops running?
Basically, i am trying to create a script that can do some changes/modifications to my database which has huge data pile. Also, this php script execution can be paused/resumed using a filelock.
You can either use set_error_handler() or register_shutdown_function().
Edit: See also: max execution time error handling
If you set 0 to set_time_limit, it will never stop :)
You can increase the limit time inside your code using:
set_time_limit(300); //time in seconds
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
or
changing the max_execution_time parameter inside your php.ini.
The default time is 30 seconds.