0

I have Lubuntu 20 running with latest Apache2 and PHP8.1.

I realized using "shell_exec" it only works for commands like "dir" or "echo" or "touch". But i need to run a command i installed into /usr/local/bin. Calling that one simply makes shell_exec return nothing.

I am also appending "2>&1" at the end of the command, but no luck.

Folders and files are owned by the same user i am running apache with, chown and chmod is fine. It seems it is blocking such commands, while systems commands are working from PHP.

PHP does not block any of the functions. Any idea why it is failing to run the command?

Update:

Non-working examples

echo shell_exec('/usr/local/bin/sfdx --help 2>&1');
echo shell_exec('/sfdx --help 2>&1');
echo passthru('/usr/local/bin/sfdx --help 2>&1');

Running all these on CLI on my Lubuntu works fine even with "php -r" command using the CLI mode of php works. Made a diff of apache php.ini and the cli one, only difference is my display_error and memory_limit setting.

Also tried with popen with no luck, it simply returns nothing.

$cmd = "/usr/local/bin/sfdx --help";
$handle = popen($cmd . " 2>&1", 'r');
$read = fread($handle, 2096);
pclose($handle);

Working examples

echo shell_exec('dir 2>&1');
echo shell_exec('touch test.log 2>&1');
11
  • Can you provide a sample of how you are running it? Are you specifying the full path the executable? Commented Sep 29, 2022 at 14:05
  • added some non working examples. my php.ini is the default one but with memory_limit=256m. no disabled functions Commented Sep 29, 2022 at 15:23
  • Instead of echo, try using var_dump to see what kind of "nothing" it really it. Can you confirm error reporting is cranked all the way up on your web version? When you run the commands from the CLI, PHP is run as your user account, however when you run it through the web version, usually a different user is used. I also like to steer people towards proc_open, which, although more verbose, it allows you to inspect stdout and stderr independently which can sometimes help. Commented Sep 29, 2022 at 17:05
  • Ok interesting, var_dump shows me "NULL". But i know the command MUST return something, also tried on CLI. So that means something goes wrong, but why "2>&1" does not give me the error? What is missing? Commented Sep 29, 2022 at 20:59
  • I honestly only ever use proc_open, so I unfortunately can't answer why the redirection stuff doesn't work. According to the docs for shell_exec, however, "This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required. Have you tried that? Commented Sep 29, 2022 at 21:24

1 Answer 1

0

Solution was to install "sfdx-cli" locally and not globally.

When it is installed globally (npm install -g sfdx-cli) then PHP is for whatever reason not able to run it. No errors returned even when shell_exec returns NULL, which means "error".

After installing it locally and running with "npx sfdx" everything works as expected.

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.