dongyi3616 2013-02-08 04:31
浏览 16
已采纳

从PHP脚本调用“php”命令导致奇怪的进程行为

I just moved a site from one host to another. The server environment is very similar (LAMP stack) and all the code worked when it got transferred, except one line. I've mutated it a bit for testing and am still getting very odd results:

<?php
$out = `php ../test/test.php 123 abc`;
?>

When running php ../test/test.php 123 abc from the command line in SSH, it works fine, as expected. And when I run: php testrunner.php (the file which has only the "$out" line above in it) in SSH, it also works as expected.

But once I load testrunner.php from the browser, it just hangs. Using ps aux | grep php to monitor the processes, processes seem to spawn up and die down (truncated for brevity):

myuser   12790  0.0  0.3 259016 45284  . . . 0:00 php ../test/test.php 123 abc

If I modify the "$out" line to be:

<?php
$out = `php ../test/test.php 123 abc &`;
?>

then I cause that script to run in the background. Surprisingly, a few seconds later when I run ps aux | grep php again, it shows the same stuff but with a new PID. I keep running ps aux and keep seeing it with a different PID. This continues for quite some time (several seconds, maybe even a minute).

This is very odd to me, since test.php only has a line to echo some text for testing purposes.

Works fine from the terminal. Hangs and has other weird behavior when invoked from the web. Am I missing something?

(I have evidence by redirecting output to a log file that, when run from the web browser, the PHP script seems to invoke ITSELF instead of the other script, test.php. And when it behaves like this, it doesn't receive any $argv parameters... but when I run it from the command line, all is well! Strange?)

UPDATE: Geez... I was just watching the server processes and the PHP ones of test.php started spawning out of control. They multiplied into the hundreds, maybe thousands, of processes: the server was brought down for a minute, SSH and everything. It's back up now, but I can't explain what's going on. There's no loops in the code and both the files involved are super-simple, isolated for testing purposes...

I'm working with my host as they respond to my support ticket, to see if this is environment-related or what... what could cause this to be happening, simply by changing the server environment?

  • 写回答

2条回答 默认 最新

  • doukefu1361 2013-02-08 16:06
    关注

    My host, A Small Orange, has been helpful, but in the end, all I or they can figure is (from my support ticket):

    ... that SuPHP or some other security-based software we have running as part of our stack is preventing your processes from spawning new processes (because that behavior can be insecure for obvious reasons) ...

    In any case, the scripts work fine on my Macbook (very different configuration with nginx) and on my old host's LAMP stack, which ASO has a similar setup.

    Perhaps I will ask about spawning long-running processes without invoking the command line so that the calling script isn't blocked in another question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?