2

I installed aws cli on my local Ubuntu 18.04 development machine. It works fine from the terminal for example if i type:

$which aws I get: /usr/local/bin/aws. However, if i try to run this command from within a php script it does nothing.

$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.

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.

Can someone explain how to get this working on ubuntu 18.04 with php?

4
  • You didn't show us any error, we cannot help you if you don't provide some info. Are you sure it does nothing? Neither of those commands would show any output and you're not printing anything from $exec2 or $output. Commented Aug 30, 2018 at 11:35
  • exec2 is null exec does returns 127 Commented Aug 30, 2018 at 12:52
  • looks like it was working all along i had redirect stdout to stderr using 2>&1 at the end of the command. Commented Aug 30, 2018 at 13:50
  • Possible duplicate of Executing AWS CLI command from php results in Unable to locate credentials, AWS CLI command works on Bash, but not with PHP shell_exec(), etc. Commented Aug 31, 2018 at 6:19

1 Answer 1

4

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.

 $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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.