Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause a PHP script to simply stop while creating a text variable?

Tags:

php

mysql

I am having the weirdest issue that I've been trying to track for months now. I've added lines and lines of debugging code that create log entries in a MySQL based log, and the result makes no sense.

Basically, the script simply stops sometimes. Sometimes it does so randomly, then it does it a dozen times in the same location, then it might continue all the way through, then it does it again the next time.

More details:

Every 15 minutes, I am looping through a list of clients, and every client has a list of data that needs to be parsed and collected for emails. If a previous version of this script is already running (i.e. a log entry exists that is less than 5 minutes old), the script is not executed again. So if I see a break in log entries longer than a few minutes and then another start 15 minutes after the first start, I know something is wrong. The most bizarre situation from the log is as follows:

I put into the log that I am about to create the database query for client X. Then I create a variable with SQL code that contains the client id and the day of the week ( date("l", strtotime("now")) ). Then I log that the query was created successfully. Note that the query ONLY exists in a PHP variable and was NOT submitted to MySQL yet!

So let me give you an example of what I see in the log:

  • 3:00:00pm - (script starts)
  • 3:00:00pm - (it loops through clients)
  • 3:00:04pm - (it has gone through some clients and is now working on client 20)
  • 3:00:04pm - creating query for client 20
  • (log ends here until the script is automatically restarted 15 minutes later if there has not been any log entry for at least 5 minutes)
  • 3:15:00pm - (script starts)
  • 3:15:00pm - (it loops through clients)
  • 3:15:04pm - (it has gone through some clients, is skipping client 20 because that obviously had a problem, and is now working on client 21)
  • 3:15:04pm - creating query for client 21
  • 3:15:04pm - successfully created query for client 21
  • (log ends here until the script is automatically restarted 15 minutes later if there has not been any log entry for at least 5 minutes)
  • 3:30:00pm - (script starts)
  • 3:30:00pm - (it loops through clients)
  • 3:30:04pm - (it has gone through some clients and is now working on client 20)
  • 3:30:04pm - creating query for client 20

And rinse and repeat. Now for a few hours, it will alternate between failing just before it creates the query for client 20 and failing just after it created the query for client 21. Then, suddenly, it might make it all the way through the rest of the clients. Then the script starts again and will continue the same weird loop. And every day or so, this will happen with one or two other clients.

The query is simple enough, something like this:

$sql = "
select fldClientName
from client
where fldClientId = $clientId
and fldEmail".date("l", strtotime("now"))." = 1
";

Basically, if today is Monday, it should check if fldEmailMonday is set to 1 to let us know that this client needs to be emailed today.

This works for tons of our clients, but it just randomly gets stuck at one or two clients that change from day to day. And again, this happens BEFORE $sql is submitted to MySQL! We get stuck in the creation of the variable $sql.

Granted, the actual query is much more complex than what I've written here, but $clientId and date("l", strtotime("now")) are the only variable parts in an otherwise static piece of text.

Furthermore, we've had the same issue over years (I've only now started tracking it down more), and by now, we have gone through three PHP servers and two MySQL servers - and we're still having the same issue, so we're moderately sure it's not a hardware issue (like memory or hard drive).

I don't know if this might be the problem, but this script is run by a cron job that starts it in lynx. This was established before I took on the code, and I don't know the reason for it. Whenever we run it manually (I usually use php index.php instead of lynx hostname://index.php), it doesn't seem to fail, ever.

So could this be an issue with Lynx? If so, why does it work 70% of the time and fail otherwise? Why the randomness?

Or is there a PHP issue that I should figure out? We're running version 5.3.2 (yes, a bit old, but our server admin doesn't want to mess with it unless absolutely necessary).

I am guessing that Lynx is used so we get the Apache log (which by the way is empty except for some deprecated code warnings that I'm trying to get rid of now, too), and I am guessing that if I run php index.php, I don't get an apache log. Maybe there's another log file I'm missing that might help here?

Also, it can't be the logging code itself that's causing it because it only submits plain static text and the client id that is established before the SQL code is created.

The script fails in plenty more locations, but this is the one that really makes no sense to me.

Any thoughts at all on what could cause something like this?

Any thoughts on what else I can do to track it down? I mean, I'm logging right before and after I create a variable, and it dies in between - now sure how much more I could be logging here...

like image 558
semmelbroesel Avatar asked Nov 25 '13 19:11

semmelbroesel


People also ask

What is get method in PHP?

There are two methods in PHP to collect data submitted in a FORM. GET Method and POST Method. In the GET method, you submit data from HTML FORM/collected using a super global variable $_GET. This method sends the encoded information from the FORM through the webpage URL.


1 Answers

It might be your issue is not related to PHP, MySQL or Apache, might be more of server environment case, some OS freezing and dropping zombies case. Did you try that cronjobs in any other environment than Lynx? i mean maybe locally or at some other server.

Have you checked up server logs for any alert/error messages?

like image 191
Sergey Sid Avatar answered Oct 13 '22 19:10

Sergey Sid