PHP 没有执行 ps 命令(可能是权限问题?)

PHP isn't executing ps command (possible permission issue?)

我有一个使用 php test.php.

在命令行上测试的小脚本

test.php

<?php
    exec('ps -acux | grep test', $testvar);
    print_r($testvar);
?>

这很好用。我能够 运行 脚本并获得所需的结果。但是,当我将代码添加到我的 PHP 服务器正在 运行 的文件中时,结果为空。

我的 OS 是 FreeBSD。查看 psman page,我看到的唯一限制是 -a 选项。它指出:

If the security.bsd.see_other_uids sysctl is set to zero, this option is honored only if the UID of the user is 0.

我的 security.bsd.see_other_uids 设置为 1。

$ sysctl security.bsd.see_other_uids
security.bsd.see_other_uids: 1

我唯一能想到的是,当我通过命令行 运行 时,我的用户正在 运行 命令,而当 运行 由 PHP,运行 被 www。我在 ps 的手册中没有看到任何表明 www 不应该能够 运行 命令的内容。

根据评论,您需要将 stderr 重定向到 stdout。这是通过 2>&1 到命令末尾来实现的。所以 PHP exec() 的例子如下:

exec('ps -acux | grep test 2>&1', $testvar);

这可以让您看到潜在的错误。就我而言,没有错误,可能是某种错误。但是,这在您执行命令的所有情况下都很有用。如果发生任何错误,您可以检索它以查看您的命令发生了什么。