无法从php中的exec或shell_exec执行aws cli命令

问题描述 投票:-1回答:1

我在我当地的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上运行吗?

php linux amazon-web-services ubuntu aws-cli
1个回答
0
投票

我的代码一直在努力。问题是我必须在命令末尾使用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);
© www.soinside.com 2019 - 2024. All rights reserved.