69

I need to run a script into localhost (xampp) which will generate 14400 records and adds them into database, I have set the max_execution_time = 50000000000, I dont know if I can make it unlimited by setting it to 0 or -1. But I tried this script before with this max_execution_time to 50000000000 and yet it stoped at certain point, I dont know what else could limit the execution time, I have been running this a lot of times and I am tired of waiting and failing again, what should I change before I run this script again and this time to finish the job?

TooCooL
  • 20,356
  • 6
  • 30
  • 49
  • 2
    call it from the command line, no time limit that way –  Apr 02 '13 at 23:42
  • 2
    `set_time_limit(0)` at the begining of your script will disable the time limit. But you can have other reason for a premature stop. It can be a problem with your DB or a problem with the memory (if you are creating a lot of objects). Check the error log of PHP. – MatRt Apr 02 '13 at 23:43
  • Where does it stop (how long or how many rows get inserted)? Same each time or does it fluctuate? Anything in PHP error logs? – Brock Hensley Apr 02 '13 at 23:44
  • I checked the for PHP errors in the php folder of xampp but it didnt show anything at the time the script stopped, everytime it adds around 8 000 records – TooCooL Apr 02 '13 at 23:45
  • do you create PHP object for each row ? do you you add your row in database one by one ? can you post your code please, it will help – MatRt Apr 02 '13 at 23:49
  • how is it hosted? a lot of hosts have various (time\memory\cpu\threads etc) restrictions. –  Apr 02 '13 at 23:50
  • 1
    You should show your code. My guess is that query execution time is not your problem if you set the value really high and it still stopped. My guess is you might have a memory issue. – Mike Brant Apr 02 '13 at 23:51
  • I am using prestashop and I am adding attribute combinations, I cant do it live because of restrictions so I am going to do it locally then add the table values into the live site, presta shop has really big issue with having more then 2000 combinations but I dont know what else to do.. – TooCooL Apr 02 '13 at 23:56

5 Answers5

176

You'll have to set it to zero. Zero means the script can run forever. Add the following at the start of your script:

ini_set('max_execution_time', 0);

Refer to the PHP documentation of max_execution_time

Note that:

set_time_limit(0);

will have the same effect.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    what if I set it into php.ini file to 0? will it take effect? – TooCooL Apr 02 '13 at 23:47
  • 5
    Yes, but I would not set this globally. This would mean that for example an endless loop (of and untested script, or a hack) can hang up the server. I would use `max_execution_time=0` with caution and only if required. – hek2mgl Apr 02 '13 at 23:48
  • I need to run it once localy it doesnt metter I will change it later, its important that it should work and not stop the script before it finishes adding the records. – TooCooL Apr 02 '13 at 23:50
  • I cannot put this at the start of the script. It only needs to be put at a specific condition. Can i put set_time_limit at some other place in the script? – Kanishk Dudeja Sep 28 '15 at 11:20
  • Yes, you can put it where ever it makes sense. – hek2mgl Sep 28 '15 at 15:44
  • Beware that set_time_limit(0) won't be honored if your script tkes time to execute external commands via system() or exec()/shell_exec() functions as stated in the doc. Also safe_mode must be off if you're PHP <= 5.3 – Marco Marsala Nov 17 '15 at 09:25
  • Didn't work! I used -1 and then only it works! Cheers – Nobody Apr 09 '22 at 10:21
2

Your script could be stopping, not because of the PHP timeout but because of the timeout in the browser you're using to access the script (ie. Firefox, Chrome, etc). Unfortunately there's seldom an easy way to extend this timeout, and in most browsers you simply can't. An option you have here is to access the script over a terminal. For example, on Windows you would make sure the PHP executable is in your path variable and then I think you execute:

C:\path\to\script> php script.php

Or, if you're using the PHP CGI, I think it's:

C:\path\to\script> php-cgi script.php

Plus, you would also set ini_set('max_execution_time', 0); in your script as others have mentioned. When running a PHP script this way, I'm pretty sure you can use buffer flushing to echo out the script's progress to the terminal periodically if you wish. The biggest issue I think with this method is there's really no way of stopping the script once it's started, other than stopping the entire PHP process or service.

Peter Cullen
  • 896
  • 1
  • 11
  • 23
2

"hek2mgl" answer above didn't work for me, so I used -1 i.e.,

ini_set('max_execution_time', -1);

Hope it helps someone.

Nobody
  • 360
  • 6
  • 9
1

As @Peter Cullen answer mention, your script will meet browser timeout first. So its good idea to provide some log output, then flush(), but connection have buffer and you'll not see anything unless much output provided. Here are code snippet what helps provide reliable log:

set_time_limit(0);
...
print "log message";
print "<!--"; print str_repeat (' ', 4000); print "-->"; flush();
print "log message";
print "<!--"; print str_repeat (' ', 4000); print "-->"; flush();
LeonidMew
  • 420
  • 5
  • 24
0

The following code in the .htaccess file worked for me:

php_value max_execution_time 0
RewriteEngine On
RewriteRule .* - [E=noconntimeout:1]
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<IfModule mod_php5.c>
#Session timeout
php_value session.cookie_lifetime 10
php_value session.gc_maxlifetime 10
</IfModule>
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
Yahya B
  • 1
  • 1