没有从包含 exec() 的 do while 循环中出来

发布于 2024-10-06 20:17:59 字数 376 浏览 0 评论 0原文

我有一个 php 脚本,它会触发三个 exec() 命令并获取它们各自的 pid。

我想等待所有三个 exec 命令停止运行,然后再进行下一批三个命令。

我尝试在 do while 循环中检查每个 pid,但它似乎永远不会脱离循环:它只是坐在那里直到脚本超时。

这是我尝试过的简化版本:

do {
 $pid = 722;
 print "checking";
 exec("ps -p $pid", $output);
 $count = count($output);
} while ( $count >= 2 );

print "out of do while";

有谁知道什么可能导致它永远不会退出循环?

I have a php script which fires off three exec() commands and gets their respective pids.

I'd like to wait for all three exec commands to stop running before moving onto the next batch of three.

I've tried checking each pid in a do while loop, but it never seems to come out of the loop: it just sits there until the script times out.

Here's a simplified version of what I tried:

do {
 $pid = 722;
 print "checking";
 exec("ps -p $pid", $output);
 $count = count($output);
} while ( $count >= 2 );

print "out of do while";

Does anyone have any ideas about what might cause it to never come out of the loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2024-10-13 20:17:59

PHP 的 exec 只是不断追加到数组中,因此计数只会增加。要覆盖输出数组,请在调用 exec 之前调用 unset($output)

有关详细信息,请参阅 关于 exec 的 php 手册

PHPs exec just keeps appending to the array, so the count will only increase. To overwrite the output array call unset($output) before calling exec.

See the php manual on exec for more details.

影子的影子 2024-10-13 20:17:59

如果输出参数存在,
那么指定的数组将是
充满了输出的每一行
命令。尾随空白,例如
作为 \n,不包含在此数组中。
请注意,如果数组已经
包含一些元素,exec() 将
附加到数组末尾
。如果你
不希望附加该函数
元素,对数组调用 unset()
在将其传递给 exec() 之前。

来自 exec() 手册

If the output argument is present,
then the specified array will be
filled with every line of output from
the command. Trailing whitespace, such
as \n, is not included in this array.
Note that if the array already
contains some elements, exec() will
append to the end of the array
. If you
do not want the function to append
elements, call unset() on the array
before passing it to exec().

From exec() manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文