Long PHP script and 500 Internal Server Error

There is a PHPscript that runs from the browser, the execution time is several hours.

About 20 minutes after launch, 500 Internal Server Errorcrashes in the browser, while the script itself continues to work, successfully completes everything and only the last line (writing to the log file) is not executed.

max_execution_timeset to 360000.
The script eats 40-50% of the processor.

What could be the problem?

Answer 1, authority 100%

Try digging into AJAX technology. On the HTML page, make an AJAX request using JS, on the server, let the PHP script give some kind of response. JS to HTML will receive the response itself and display it on the HTML page.

This way you will be 100% aware of the state of PHP script execution. (Well… at least you will be aware of its implementation).

Now on previous comments:

Where is max_execution_timeset? Did you expose via php.ini or via .htaccess?

The browser has a time limit to wait for a response from the server. So it’s not about these variables. Especially since the person says that the script is fully executed.

@kirelagin such questions are rarely relevant for the PHP public πŸ™‚

You shouldn’t think so πŸ™‚ …I, as a developer, have already encountered this and often encounter it to this day πŸ™‚

Does it make sense to send browsers some HTML comment every 19 minutes?

In any case, PHP generates HTML content. If you send the code after 19 minutes, it is not a fact that it will be seen on the side.

My bottom line is: AJAX is here to help πŸ™‚

P.S. Google ajax.js. You can also get by with jQuery.

Answer 2, authority 50%

Most likely, your web server running the script is backend to another web server. A good example is nginx+apache. The 500 error occurs because when you make a request, nginx starts processing it, sees the dynamic content that apache should process, redirects the request to it and waits for it to complete its work, or after a timeout, in in your case, – 20 minutes, gives the user a 500th error. In this case, the instructions max_execution_timewill not help, because they are directed to apache, not nginx.

P.S. you can solve the problem:

  1. run script as cgi as nginx,
  2. remove timeout when passing through nginx.

P.P.S. As for writing to the log file, I would advise to catch the script’s error stream and write it to a file, most likely some kind of error will be revealed there, for example, a file access error.

Answer 3, authority 25%

Send the actions to be performed to the browser, for this there were php functions that allowed you to send content on the go, I don’t remember which ones – see the documentation.
Also try increasing the response time.
Try running the script in a separate process rather than through a web server.

Answer 4, authority 25%

By the way, yes, every 19 minutes do:

print " ";

and perhaps help.