I need to one of my script (not all) works only five second. And if execution not finished yet (within five seconds) it should be dropped.
So I use
ini_set('max_execution_time', 5);
Also if I do
ini_get('max_execution_time');
it shows me five second, but script not interrupt after 5 seconds.
P.S
safe_mode = off
nginx -> php-fpm
set_time_limit(5) also has no effect
You can use set_time_limit function on top in your code as follows:
set_time_limit(5)
NB: If you dont place it on top of your code, supposed the php script has ran for 3 seconds and you called set_time_limit(5) so the total time of allowed execution would be 3+5 = 8 sec not 5sec as expected
Update
From php documentation:
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
where the measured time is real.
Related
Why php or apache not stop infinite loop?
while (true)
{
sleep(5);
}
Apache have default TimeOut = 300.
Php - max_execution_time = 30
But this loop can work over hour. Why?
Since sleep() happens outside of your script, it doesn't contribute to the max execution time of your script.
Note: 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(), the sleep()
function, database queries, etc. is not included when determining the
maximum time that the script has been running.
http://php.net/manual/en/function.sleep.php
One of my PHP scripts is throwing the following error at me:
Fatal error: Maximum execution time of 30 seconds exceeded
The problem is that the script has only been executing for a few seconds.
I've timed everything between 2 and 10 seconds.
set_time_limit and ini_set('max_execution_time') have no effect (because of PHP safe mode).
Now I could just increase the value in my php.ini , but I would like to know why this error is thrown within a fraction of the actual maximum execution time.
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.
Or You can user set_time_limit(0) it will help only safemode.
This means that you might have an infinite loop somewhere or there is some code that has a timeout higher than 30 seconds.
First of all the script take sometimes to execute and it stops after 30 seconds. Says that the execution time is max about 30 seconds (I know that we can change this param in the httpconf)
but I don't know the max execution time may be 1 hour or more.
So I want to know if there is no time limit execution in php?
Second question: how to show the content in a browser when it's ready and continue the execution, because I always wait the script to end in order to see the content displayed in the browser.
Next time you post a question, make sure to research it better. Both questions are well documented on stackoverflow and the internet.
now to your answer...
To let it run until finished regardless of the time it takes:
http://www.php.net/manual/en/function.set-time-limit.php
set_time_limit(0);
To display content right after it is generated:
http://www.php.net/manual/en/function.flush.php
ob_flush();
flush();
A script that might run that long should be run from the console. Browser timeouts could also interfere with running it via browser.
If you use a console script, you could route the output into a database and query it from there.
You have in php.ini this code. I am not sure you could check it from elsewhere than php.ini.
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30
You can't do that in php. The script runs until it finishes execution (normal, or error).
im getting php maximum execution time like this.
<?php ini_get('max_execution_time'); ?>
output: 30
but i can run this script.
<?php
sleep(200);
echo "no timeout error";
?>
output: no timeout error
im how to get real ini value ?
best regards
From the manual:
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.
Since sleep is a system call, it doesn't count against the max execution time.
Your max_exec_time IS 30 seconds, but it's not timeouting because time is not running in sleep() but only when script is doing something
How can I rewrite this into a cron that will run every day for longer than 30 seconds? Also, do I need to edit the .htaccess or php.ini file in the cron.php directory to say something? Over the browser it runs just fine for longer than 30 seconds; over the shell, it runs just fine too. But as a cron set task, it dies after 30 seconds. I'm on 1and1 share hosting.
0 12 * * * php5 /this/is/the/file/cron.php
There are several things that could be terminating your script. One could be the maximum execution time set in the php.ini file. If that's the case, you can override it in your script with set_time_limit(0); where zero means no limit and any number greater than zero is the number of seconds to allow the script to run for before being terminated. It's important to note that this time does NOT include the time it takes for the browser to make the request, so file upload time wouldn't count here.
If you're in a shared hosting environment (like Dreamhost), they have process watches that will kill off any PHP process after a set time limit. You cannot get around these. You would need to contact the hosting provider to see what you need to do to get access to run the script for longer (for Dreamhost, they want you to have a they're PS offering).
Use this syntax to start php:
php -c /path/to/another/php.ini /this/is/the/file/cron.php
Then you can specify a different timeout (or no timeout) in a different php.ini file.
ini_set('max_execution_time', 600);
Add this to the top of your php file and it will run for 600 seconds. Anything more is not recommended but you can have a go if you want.
You don't need to set a higher max_execution_time if you use PHP CLI:
CLI SAPI default value for "max_execution_time" is set to unlimited.
http://nl3.php.net/manual/en/features.commandline.differences.php
I would just use wget http://path.to.myscript.php
If it's dying after 30 you may need to set max_execution_time = 60 in your php.ini to allow the script to run longer than 30 seconds.
You could also use ini_set('max_execution_time', 60)
But as the manual page says, in some cases (i.e. running in safe mode) this will have no effect at all: http://uk.php.net/manual/en/info.configuration.php#ini.max-execution-time
It could also be possible that the php.ini for client line has different max execution values than for the browser. I have seen it sometimes.