I'm running a php script via Linux (not via my browser) (php script.php).
Now, I want to limit the execution time to let's say 10 seconds, which means that after 10 seconds the process will shutdown by itself. How can I do that?
Already tried set_time_limit.
Already tried sleep(10); exit();
None of them made the script to shutdown.
Change the maximum execution time (in seconds) in php.ini configuration file.
max_execution_time = 7200
search in php.ini
What about using set_time_limit function? You can check documentation here
function
void set_time_limit ( int $seconds )
Related
This question already has an answer here:
Increase PHP Script Execution Time [duplicate]
(1 answer)
Closed 9 years ago.
I want to increase maximum execution time in php , not by changing php.ini file.
I want to Increase it from my php file.
Is this possible?
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0'); // for infinite time of execution
Place this at the top of your PHP script and let your script loose!
Taken from Increase PHP Script Execution Time Limit Using ini_set()
use below statement if safe_mode is off
set_time_limit(0);
Use the PHP function
void set_time_limit ( int $seconds )
The maximum execution time, in seconds. If set to zero, no time limit is imposed.
This function has no effect when PHP is running in safe mode. There is
no workaround other than turning off safe mode or changing the time
limit in the php.ini.
You can try to set_time_limit(n). However, if your PHP setup is running in safe mode, you can only change it from the php.ini file.
I have a large file to insert into phpmyadmin,
I wrote a query that reads from file and inserts into db,
it works fine , but inserts only about 65 rows then stops .
It might be a case of execution time out for php.
Can you have a try setting up the max_execution_time through set_time_limit function.
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
Warning:
This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.
http://php.net/manual/en/function.set-time-limit.php
This question already has an answer here:
Increase PHP Script Execution Time [duplicate]
(1 answer)
Closed 9 years ago.
I want to increase maximum execution time in php , not by changing php.ini file.
I want to Increase it from my php file.
Is this possible?
ini_set('max_execution_time', '300'); //300 seconds = 5 minutes
ini_set('max_execution_time', '0'); // for infinite time of execution
Place this at the top of your PHP script and let your script loose!
Taken from Increase PHP Script Execution Time Limit Using ini_set()
use below statement if safe_mode is off
set_time_limit(0);
Use the PHP function
void set_time_limit ( int $seconds )
The maximum execution time, in seconds. If set to zero, no time limit is imposed.
This function has no effect when PHP is running in safe mode. There is
no workaround other than turning off safe mode or changing the time
limit in the php.ini.
You can try to set_time_limit(n). However, if your PHP setup is running in safe mode, you can only change it from the php.ini file.
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.