Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script stops running arbitrarily with no errors

I have a PHP script that seemed to stop running after about 20 minutes.

To try to figure out why, I made a very simple script to see how long it would run without any complex code to confuse me.

I found that the same thing was happening with this simple infinite loop. At some point between 15 and 25 minutes of running, it stops without any message or error. The browser says "Done".

I've been over every single possible thing I could think of:

set_time_limit ( session.gc_maxlifetime in the php.ini)
memory_limit
max_execution_time

The point that the script is stopped is not consistent. Sometimes it will stop at 15 minutes, sometimes 22 minutes.

Please, any help would be greatly appreciated.

It is hosted on a 1and1 server. I contacted them and they don't provide support for bugs caused by developers.

like image 544
RobHardgood Avatar asked Jan 18 '10 05:01

RobHardgood


People also ask

How do I make PHP scripts run forever?

You can make it run forever by either setting the value or call set_time_limit in your script (http://php.net/manual/en/function.set-time-limit.php).

Why is my PHP script not working?

You have to open it using localhost url. if you open a file directly from your directory it will not execute the php code in any case. Enable php short code.

Can a PHP script run forever?

Whenever a PHP application rebuilds MySQL indexes, the process may run for a long time. Generally, allowing a PHP script to run forever is not desirable. Thus there are a number of features (in LiteSpeed Web Server and built into PHP itself) that may prevent a PHP process from running long enough to finish.

How can the execution of a PHP script be stopped?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.


2 Answers

At some point your browser times out and stops loading the page. If you want to test, open up the command line and run the code in there. The script should run indefinitely.

like image 160
James Hartig Avatar answered Sep 18 '22 12:09

James Hartig


Have you considered just running the script from the command line, eg:

php script.php

and have the script flush out a message every so often that its still running:

<?php

while (true) {
  doWork();
  echo "still alive...";
  flush();
}
like image 20
AndrewMurphy Avatar answered Sep 21 '22 12:09

AndrewMurphy