Home > Blockchain >  php shell_exec uses bourne shell regardless of what i am doing
php shell_exec uses bourne shell regardless of what i am doing

Time:01-14

when i call shell_exec as the following, it doesn't actually call the php but does sh: and file not found.

shell_exec('"/usr/bin/php ./magento xigen:clicreatecustomer:create -f \"' . $contacts['name'] . '\" -l \"company\" -e \" ' . $contacts['email'] . '\" -p \"test123\" -w 1 --street \"' . $address['Address1'] . '\" --city \"' . $address['City'] . '\" --region \" ' . $address['State'] .' \" --telephone \"00000000\" --postcode \" '. $address['Zip'].' \" -c \"' . $customer['entityid'] . '"');

what am I doing wrong? i have tried doing absolute paths also.

CodePudding user response:

Try using an absolute path to magento in your command, that could be what sh is choking on.

EDIT:

Two things:

  1. Try echoing one line from your script and executing it from the shell manually to determine where your shell command is failing. This may help solve your problem entirely.
  2. If you are still having problems running the code through PHP, instead of running the commands through shell_exec, write them all to a separate file, then source that file from the command line.

CodePudding user response:

You're adding too many quote signs.

$cmd = "/usr/bin/php ./magento xigen:clicreatecustomer:create -f '{$contacts['name']}' -l company -e '{$contacts['email']}' -p 'test123' -w 1 --street '{$address['Address1']}' --city '{$address['City']}' --region '{$address['State']}' --telephone '00000000' --postcode '{$address['Zip']}' -c '{$customer['entityid']}'";

shell_exec($cmd);

Therefore, the shell tried to execute a file called "/usr/bin/php\ ./magento\ xigen...", and that file, of course, does not exist.

Note that if the address or any other field contains an unquoted single quote, the command will fail. You need to replace "'" with "\'" in those fields before running the command.

To debug what is happening, print the contents of the $cmd variable, or save it to a file and inspect it.

  •  Tags:  
  • Related