代码之家  ›  专栏  ›  技术社区  ›  user794846

无法在php中从exec或shell exec执行aws cli命令

  •  -1
  • user794846  · 技术社区  · 7 年前

    $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 返回空exec返回127。

    现在我知道这是一个路径问题,我试图将它添加到路径中,但它不起作用。

    我知道这可以使用php sdk完成,但由于各种原因,我不能使用它。

    有人能解释一下如何用php在ubuntu 18.04上运行吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   user794846    7 年前

    我的代码一直在工作。问题是,我必须在命令末尾使用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);
    
    推荐文章