Home > Enterprise >  php exec command with azure cli always return null
php exec command with azure cli always return null

Time:03-27

On a server Apache which runs PHP, I have a command which always return null.

exec("az storage container exists --account-name $accountName --account-key $key --name $containeurName", $output);

I modified the sudoers file but it didn't change anything:

www-data ALL=(ALL) NOPASSWD: /usr/bin/az

Thanks for your help.

CodePudding user response:

Use exec($your_command, $output, $error_code) and see what $error_code contains. It may just be because az isn't in the PATH env variable in PHP.

Try to put the full path to your executable, typically something like this:

<?php

// A default path in case "which az" doesn't work.
define('AZ_DEFAULT_PATH', '/usr/bin/az');

// Find the path to az with the "which" command.
$az_path = exec('which az');
if ($az_path === false) {
  $az_path = AZ_DEFAULT_PATH;
}

// Sanitize your variables to avoid shell injection and build the command.
$cmd = $az_path .
       "storage container exists --account-name $accountName " .
       "--account-key $key --name $containeurName";

$last_line = exec($cmd, $full_output, $error_code);

// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
  '$cmd' => $cmd,
  '$last_line' => $last_line,
  '$full_output' => $full_output,
  '$error_code' => $error_code,
]);
  • Related