我在本地 Ubuntu 18.04 开发计算机上安装了 aws cli。它可以在终端上正常工作,例如如果我输入:
$which aws
我得到:/usr/local/bin/aws
。但是,如果我尝试从 php 脚本中运行此命令,它不会执行任何操作。
$cmd ="aws --version";
try {
$exec = exec($cmd,$output, $return);
$exec2 = shell_exec($cmd);
}
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
exec2
返回 null exec 返回 127。
现在我明白这是一个路径问题,我尝试将其添加到路径中,但它不起作用。
我知道这可以使用 php sdk 来完成,但由于各种原因我无法使用它。
有人可以解释一下如何使用 php 在 ubuntu 18.04 上运行它吗?
最佳答案
我的代码一直有效。问题是我必须在命令末尾使用 2>&1 将 stdout 重定向到 stderr。
$cmd = "aws redshift restore-table-from-cluster-snapshot --cluster-identifier {$this->redshift_cluster_identifier} --snapshot-identifier {$this->snapshot_identifier} --source-database-name citnow --source-table-name {$tableName} --target-database-name citnow --new-table-name {$newTableName} 2>&1";
$exec = exec($cmd,$output, $return);
关于php - 无法在 php 中从 exec 或 shell_exec 执行 aws cli 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095669/