简体   繁体   English

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

[英]Cannot execute aws cli command from exec or shell_exec within php

I installed aws cli on my local Ubuntu 18.04 development machine. 我在本地Ubuntu 18.04开发计算机上安装了aws cli。 It works fine from the terminal for example if i type: 例如,如果我输入以下内容,则可以从终端正常工作:

$which aws I get: /usr/local/bin/aws . 我得到的$which aws/usr/local/bin/aws However, if i try to run this command from within a php script it does nothing. 但是,如果我尝试从php脚本中运行此命令,则不会执行任何操作。

$cmd ="aws --version";

try {
    $exec = exec($cmd,$output, $return);
    $exec2 = shell_exec($cmd);
}
catch(Exception $e)
{
    echo 'Message: ' .$e->getMessage();
}

exec2 returns null exec returns 127. exec2返回null exec返回127。

Now I understand that this is a paths issue and I have tried to add it to the path but it does not work. 现在,我了解到这是路径问题,并且我尝试将其添加到路径中,但是它不起作用。

I know this can be done using the php sdk but for various reasons i cant use that. 我知道可以使用php sdk完成此操作,但由于种种原因,我无法使用它。

Can someone explain how to get this working on ubuntu 18.04 with php? 有人可以解释如何使用php在ubuntu 18.04上运行吗?

My code was working all along. 我的代码一直都在工作。 The issue was that I had to redirect stdout to stderr using 2>&1 at the end of the command. 问题是我必须在命令末尾使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM