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.
Related
My website is hosted on digitalocean server and i have full control over the server. I have php script that downloads thousand of images using proxy IP. Sometimes my script takes infinite time and it slows the whole site. I checked max_execution_time and it is set to default 30 secs. But still my script doesn't stop after 30 secs. How can i stop my script from running after 2 mins so that my site doesn't effect other scripts.
The 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.
refer this link for more details.
You can try recording the timestamp at the beginning and referencing it each iteration. Use break; or return; to exit the loop
function getImagesFromRemote($urlArray=[]) {
$start = time();
$maxduration = 3 * 60 ;
foreach ($urlArray as $url) {
// do your code
$something[] = $value;
$elapsed = time() - $start;
if ($elapsed >= $maxduration) break;
}
return $something;
}
}
By using exit() function we can stop the execution of php script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.
Function, which waits about one day in the background and then execute another.
Like:
function Sleep(){
sleep( /* One Day */ );
Run();
}
function Run(){
//One Day later,
//execute code.
}
Or maby something like this (this is fictional):
class Waiter extends Timer{
$time = 0;
function __construct($time){
$this->time = $time;
}
function onDelay(){
//One day Later.
}
}
$wait = new Waiter( /* One Day */ );
Is there a good solution?
Or is the sleep() function also okey?
But I have to say, that the execution timeout is 30 seconds.
Using a cronjob is the correct solution for this problem. If for some reason you cannot use it, make sure to add ignore_user_abort(1) and set_time_limit(0); at the top of the php script.
int ignore_user_abort ([ bool $value ] )
When running PHP as a command line script, and the script's tty goes
away without the script being terminated then the script will die the
next time it tries to write anything, unless value is set to TRUE
bool set_time_limit ( int $seconds )
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 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.
As you said, the execution time is 30 seconds, the overall script is forced to an end after 30 seconds. It can't wait longer.
As already suggested, you could use a cron job.
If your problem is not regular recurrent, you could write a little script, which writes the time (with date) when the function should execute into a file (or a database). The cron then would execute every hour (or if its important, every minute) and checks, if the function needs to be executed
Crons have been mentioned, but there's a second option - queueing.
https://en.wikipedia.org/wiki/Message_queue
There is a wide variety of queue software available, from ones you install yourself like Beanstalk or RabbitMQ to hosted ones in the cloud like Amazon SQS or IronMQ.
I'm trying to modify a csv import function which times out after 60 sec of importing. For every line there are images that are resized and some other code is executed.
I know the vps can handle this but in batches because I have another website on the same server that runs a different csv program but does the same thing. That program can import 8000 lines and resize images as well. The settings there are: process 10 lines and wait 3 sec, repeat.
Settings I raised:
set_time_limit
max_execution_time
Browser http keep alive timeout
I have tried sleep() for every 10th line but this only makes the process import fewer lines
if( (($current_line % 10) == 0) && ($current_line != 0) )
{
sleep(3);
}
This is how the script loops through the file
for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++)
{
//code here
}
Server:
Apache
PHP 5.3.3
MYSQL
Varnish cache
What can I do to make this work?
The first thing to try when your script times out is to run it using the php-cli. There is no execution time limit to scripts that are run through the command line.
If this doesn't solve your problem, then you know it wasn't the execution time limit.
The second thing to try is to print out regular status messages, including from memory_get_usage() so that you can eliminate memory leaks as a cause for your script crash. This may help you identify whether your script was dying on some input.
you can over-write the default timeout time.
set_time_limit (0) ;
using sleep will make it import fewer lines, basically the script is timeing out because it is taking over 60 seconds. by adding sleep it just gets less done in 60 seconds.
If this is a critical script i'd look at moving it to another programing language that can execute this faster. if its just a one off, or not mission critical try set_time_limit(0) which makes it never time out. also try typing php scriptname into the browser to run the script in command line.
Try outputting something to the browser to keep the browser alive. IE times out after 1 minute if inactivity FF is 3 minutes.
<?
if( (($current_line % 10) == 0) && ($current_line != 0) )
{
sleep(3);
echo '. ';
}
?>
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.
i wrote a site crawler and i use while loop to crawl whole web site but my loop stop after about 660 seconds .
i set set_time_limit ( 0 ) and use flush to echo output and i use sleep function,i thought my code is wrong but i test a simple while loop in my code :
while ( 1 ) {
sleep ( 30 );
echo "Crawling on the go ..." . time ();
echo "<br />";
echo str_pad ( " ", 4096 );
flush ();
}
But this simple while stop too after about 660 seconds!i don't know what's my problem ! I checked WHM Process Manager on my server and i saw that my process killed !
i wanna know server firewall kill my process because of something like CPU or RAM usage ?
plz help me!
You need to write set_time_limit(0); at top of the page after PHP opening tag. This has worked for me .. I hope this will also work for you too..
set_time_limit() may be denyed (it has no effect if PHP is in safe_mode). On a side note, the sleep() time is not included in the time limit, so if you remove the sleep and the time script is working is changed, probably it's time limit. To check if this is a time limit (other option is CPU-time limit), register shutdown function and check connection status (value of 2 means time out)
You can also put the following line in your .htaccess php_value max_execution_time <timeinseconds>