Forum Moderators: coopster

Message Too Old, No Replies

500 Internal Server Error

         

SeanF

6:18 pm on Feb 28, 2022 (gmt 0)

5+ Year Member Top Contributors Of The Month



I have a SaaS business management suite which is written in PHP/MySQL.

One of the functions is to provide a directory listing of customers to a web page. The data comes from a number of complex SQL calls which is time consuming. So, to minimize access time, I have a PHP script which processes a number of tables and creates a large temporary table which can be parsed much more quickly.

The intention is that this script would be run by a cron job 4-6 times a day to update the temp table.

The script takes about 4-5 minutes to run on my development server but fails to finish on the production server (a dedicated server managed by dreamhost.com). After about 2 minutes I get the following error:

"Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request."

I assumed this was because it was timing out so I added the following to the php.ini file: "max_execution_time = 1500"
when I run phpinfo(); it confirms that max_execution_time is set to the desired value.

I have the following in the beginning of the code:
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
and no errors are displayed either on the development server or the production server.

What else could I be missing?

Thanks

NickMNS

6:37 pm on Feb 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is probably being caused by settings in Apache (or what ever web-server you are using).

w3dk

6:43 pm on Feb 28, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



> I assumed this was because it was timing out...

Possibly, but you need to check the details of this error in the server's error log, otherwise, you could just end up just chasing your tail.

So, after setting "max_execution_time = 1500" (25 minutes) is it still failing after "about 2 minutes" as before?

robzilla

7:04 pm on Feb 28, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To avoid timeouts and unnecessary overhead, call the script directly from the shell.

So instead of something like:

0 0,4,8,12,16,20 * * * wget https://example.com/script.php

You do:

0 0,4,8,12,16,20 * * * /usr/bin/php /location/of/your/script.php

SeanF

7:59 pm on Feb 28, 2022 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks for the replies...

Entirely by coincidence I found that if I put an "echo" command followed by an "ob_flush(); ob_clean();" statement inside the main "while" loop the script ran to completion. If I comment out the single "echo" line it fails again. What would cause this?

It appears that if there is no communication with the browser it will fail... what does that imply for the cron job which is not being called by a browser?

Also, I am not familiar with your syntax of calling the script from a cron job.

Currently the cron job is simply:
/usr/local/php74/bin/php /home/username1/domainname.com/scriptname.php

How do I interpret:
0 0,4,8,12,16,20 * * * /usr/bin/php /location/of/your/script.php?

Thanks again

phranque

11:19 pm on Feb 28, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you need to check the details of this error in the server's error log, otherwise, you could just end up just chasing your tail

this!

"Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request."

where are you seeing this error?

It appears that if there is no communication with the browser it will fail... what does that imply for the cron job which is not being called by a browser?

even if the execution time is essentially zero, the browser will time out without a response.
a cron job only needs to complete - it doesn't need any output.

phranque

11:25 pm on Feb 28, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



How do I interpret:
0 0,4,8,12,16,20 * * * /usr/bin/php /location/of/your/script.php?

that means run the specified script every 4 hours at the top of the hour [starting at midnight] every day.

[edited by: phranque at 9:33 am (utc) on Mar 1, 2022]

robzilla

9:06 am on Mar 1, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Currently the cron job is simply:
/usr/local/php74/bin/php /home/username1/domainname.com/scriptname.php

OK, so you're already avoiding the web server. That's good. Oftentimes people will have the cron job call upon a URL to execute a script, unnecessarily involving the web server.

However, your error message ("The server encountered an internal error or misconfiguration and was unable to complete your request") seems to contradict this, because that's one usually given by Apache. The PHP interpreter (/usr/local/php74/bin/php) would not give such a message.

Where are you expecting the error message for the cron job to show? Did you check your cron logs? Do you have access to the server (via SSH) or just a control panel?

Entirely by coincidence I found that if I put an "echo" command followed by an "ob_flush(); ob_clean();" statement inside the main "while" loop the script ran to completion. If I comment out the single "echo" line it fails again. What would cause this?

Would the script generate any output to buffer? In other words, does that loop give any output (if you remove the echo)? If not... I'm not sure why it would make a difference for script completion (with the cron job anyway, if you run it from a browser you can run into all sorts of limits).