PHP script stops running arbitrarily with no errors

岁酱吖の 提交于 2019-12-03 13:54:47

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.

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();
}

in such cases, i turn on all the development settings in php.ini, of course on a development server. This display many more messages, including deprecation warnings.

In my experience of debugging long running php scripts, the most common cause was memory allocation failure (Fatal error: Allowed memory size of xxxx bytes exhausted...)

I think what you need to find out is the exact time that it stops (you can set an initial time and keep dumping out the current time minus initial). There is something on the server side that is stopping the file. Also, consider doing an ini_get to check to make sure the execution time is actually 0. If you want, set the time limit to 30 and then EVERY loop you make, continue setting at 30. Every time you call set_time_limit, the counter resets and this might allow you to bypass the actual limits. If this still isn't working, there is something on 1and1's servers that might kill the script.

Also, did you try the ignore_user_abort?

I appreciate everyone's comments. Especially James Hartig's, you were very helpful and sent me on the right path. I still don't know what the problem was. I got it to run on the server with using SSH, just by using the exec() command as well as the ignore_user_abort(). But it would still time out. So, I just had to break it into small pieces that will run for only about 2 minutes each, and use session variables/arrays to store where I left off. I'm glad to be done with this fairly simple project now, and am supremely pissed at 1and1. Oh well...

I think this is caused by some process monitor killing off "zombie processes" in order to allow resources for other users.

Run the exec using "2>&1" to log anything including stderr.

In my output I managed to catch this:

...
script.sh: line 4: 15932 Killed                  php5-cli -d max_execution_time=0 -d memory_limit=128M myscript.php

So something (an external force, not PHP itself) is killing my process!

I use IdWebSpace which is excellent BTW but I think most shared hosting providers impose this resource/process control mechanism just to be sane.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!