1

I am quite new to PHP, and i need to get the output of the command 'iocage list -h', iocage is a management tool for freeBSD. Using the code:

$file = shell_exec('/usr/local/bin/iocage list');
echo $file;

It outputs nothing, if i do:

$file = shell_exec('/usr/local/bin/iocage list 2>&1');
echo $file;

Then it outputs

Usage: iocage [OPTIONS] COMMAND [ARGS]... Try "iocage --help" for help. Error: No such command "list". 

This is not right as if i run the command '/usr/local/bin/iocage list' it works as expected. I do not understand why there is a '2>&1' at the end of the command, i saw it in a an answer for a different question.

Does anyone know how to make this work? Thank you for any help in advance.

  • This has nothing to do with php. It is Standard linux for Redirect stderr to stdout. – Jens Nov 28 '19 at 21:10

1 Answers1

0

On your question about 2>&1, here is a great explanation:

In the shell, what does " 2>&1 " mean?

Now, on why your shell_exec does not work, you must make sure that the web user has proper rights. Taken from https://www.php.net/manual/en/function.shell-exec.php:

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

Hope this can shed some light to your problem.

Eric Day
  • 124
  • 1
  • 9
  • Thank you for your reply, i don't think user issues are the problem. If i run the command as www, with "sudo -u www iocage list" then it works fine, so the issue is probably not user privileges. – Piers Bowater Nov 29 '19 at 18:02