调用shell_exec("php myscript.php")将进入无限循环


calling shell_exec("php myscript.php") goes into infinite loop

我决定放弃我的php脚本,因为它运行时间太长了。当我在本地linux机器上运行shell_exec()调用时,我没有看到无限循环问题,但是在托管机器上,脚本进入了无限循环。我把代码减少到最小,我希望有人能帮助我看到这里的问题:

涉及3个脚本:
testronghell.php ->向forkphp.sh ->发出shell_exec(), forkphp.sh ->发出命令"path/to/php write_hello_world.php"

从上到下的顺序,首先是testronghell.php脚本:

<?php
    if(function_exists('shell_exec')) {
            echo "shell_exec() is enabled";
    }
    $cmd = "./forkphp.sh > /dev/null 2>&1 &";
    echo "<br/> About to shell_exec($cmd)<br/>";
    $out = shell_exec($cmd);
    echo $out;

?>

这里是forkphp.sh:
#!/bin/bash
# About to run /usr/bin/php  write_hello_world.php
echo $(/usr/bin/php write_hello_world.php)

最后,这里是write_hello_word.php:

<?php
$data = "This is a test : testing 'n testing 'n ";
file_put_contents ("deleteme.txt",$data);

?>

这会导致一个无限循环,其中文件'deleteme.txt'不断被重写。我只是猜测我可能在某个地方滥用了'$' ?提前感谢您的帮助。

FILE_APPEND标志传递给file_put_contents(),否则文件将被一次又一次地覆盖:

file_put_contents ("deleteme.txt",$data, FILE_APPEND);