调用shell_exec("php myscript.php")进入无限循环 [英] calling shell_exec("php myscript.php") goes into infinite loop
问题描述
我决定派出我的php脚本,因为它运行时间太长.当我在本地linux计算机上运行shell_exec()调用时,我没有看到无限循环问题,但是在托管计算机上,脚本进入了无限循环.我将代码减少到最低限度,希望有人可以帮助我在这里查看问题:
I decided to fork out my php script because it takes too long to run. When I ran it shell_exec() call on a local linux machine, I did not see the infinite loop problem, but on a hosted machine, the script went into an infinite loop. I reduced the code to the minimum and I hope someone can help me see the problem here:
涉及3个脚本:
test_shell.php->将shell_exec()颁发给forkphp.sh->发出命令"path/to/php write_hello_world.php"
3 scripts are involved:
test_shell.php --> issues shell_exec() to forkphp.sh --> which issues a command "path/to/php write_hello_world.php"
从上到下的顺序,首先是test_shell.php脚本:
starting from top to bottom order, first the test_shell.php script:
<?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:
Here is 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:
Finally, here is write_hello_word.php :
<?php
$data = "This is a test : testing \n testing \n ";
file_put_contents ("deleteme.txt",$data);
?>
这将导致一个无限循环,其中文件'deleteme.txt'不断被重写.我只是在猜我可能在某个地方误用了"$"? 预先感谢您的帮助.
This gets an infinite loop where file 'deleteme.txt' continuously re-written . I am only guessing that I maybe misusing the '$' somewhere? Thank you in advance for help.
推荐答案
通过 FILE_APPEND
标志标记为file_put_contents()
,否则文件将被一次又一次覆盖:
Pass the FILE_APPEND
flag to file_put_contents()
, otherwise the file would being overwritten again and again:
file_put_contents ("deleteme.txt",$data, FILE_APPEND);
这篇关于调用shell_exec("php myscript.php")进入无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!