shell_exec 返回空字符串
当我使用 shell_exec
在 PHP 中执行命令时,它总是返回一个空字符串。我尝试了 shell_exec('ls -l'); ,它有效。我将命令作为单独的文件放置,但结果仍然相同。
$shellOutput = shell_exec("pacmd list-sinks | grep 'volume: 0:' | tail -1 | awk '{ print $3 }'");
//return execute status;
echo trim($shellOutput);
When I execute my command in PHP with shell_exec
it always returns an empty string. I tried shell_exec('ls -l');
and it works. I put the command as a separate file and still same result.
$shellOutput = shell_exec("pacmd list-sinks | grep 'volume: 0:' | tail -1 | awk '{ print $3 }'");
//return execute status;
echo trim($shellOutput);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将 sudoers 更改为
ALL ALL = (ALL) :NOPASSWD ALL (非常不安全,但只是为了找到一定可以工作的东西),
然后我做了
sudo -u myusername /path/to/script.sh
where script.sh
有
导出VAR=值
为所有必要的环境变量导出 VAR=值
(您可以从可以正确 pacmd 的用户处执行 printenv 来查看您需要的内容。可能是一个好的 HOME,也可能是一个 XAUTHORITY)。
希望有帮助
I changed the sudoers to
ALL ALL = (ALL) :NOPASSWD ALL (very un-secure, but just to find something sure to work),
then I did a
sudo -u myusername /path/to/script.sh
where script.sh
has
export VAR=value
export VAR=value
for all the environmental variables that are necessary (you can do a printenv from a user who can properly pacmd to see what you need. probably a good HOME and maybe an XAUTHORITY).
hope that helps
您可能想在命令末尾尝试“> file.txt 2>&1”。它将输出重定向到一个单独的文件。
您最终会得到一个如下所示的文件:
You may want to try " > file.txt 2>&1" at the end of your command. It will redirect the outputs to a separate file.
You'll end up with a file that looks something like this:
大多数时候 php 脚本是由 Apache 运行的,如果您的脚本是这种情况,那么 Apache 用户可能没有足够的权限来运行此命令。首先检查一下。如果它作为 CLI 脚本运行,则检查 PHP 用户是否可以运行该脚本。
Most of the time php scripts are run by Apache, if thats the case with your script then Apache user may not have enough permissions to run this command. Check that first. If its run as CLI script then check if PHP user can run the script.
也许这就是为什么
尝试使用 系统
Maybe this is why that
Try to use system