Francois Combet
Francois Combet

Reputation: 326

How to exceed max_execution_time in PHP

In PHP, I want to run a script who need 2 minutes to compute but my max_execution_time is only 30 seconds.
I can't change my max_execution_time in php.ini nor call shell_exec

I already done all the optimization I could think of. Any idea?

Upvotes: 0

Views: 358

Answers (4)

Amit Verma
Amit Verma

Reputation: 41249

You can also set it in your .htaccess file

Add the following line to your .htaccess file

php_value max_execution_time 200 

Upvotes: 2

Martin Magakian
Martin Magakian

Reputation: 3766

The "best" option you have is to cut your algorithm into smaller part. Each part should execute within 30 second to avoid timeout. Here are few ideas.

Using CURL:

  • [Script Start] Call run.php let is compute for 20s.
  • Save it stat into a database with an ID
  • Make a CURL call run.php?resumeID=384 [Script finish]
  • [Script Start] retrieve the stat and continue the computation
  • and so on...

Using CRON task:

  • call run.php every second. It will pick a task in your database then flag as "in progress". So next time run.php is call it will not pick the "in progress task"
  • After 20s of computation, save the stat of your script in database and change the "in progress" flag to false.
  • Next call to run.php will pick the task and continue the processing
  • and so on...

Upvotes: 2

Sameer K
Sameer K

Reputation: 799

Use like

ini_set('max_execution_time', 100);

Upvotes: 1

tim
tim

Reputation: 2039

Even if you can't change the php.ini file itself, you might be able to change options via PHP code with ini_set.

You can also try to call set_time_limit inside your code, resetting the current time counter (this doesn't work in safe mode).

If that doesn't work, try to split the task up into multiple sub-tasks and save the temporary results, so that you can perform the task on multiple executions and thus bypass the time limit.

Upvotes: 2

Related Questions