3

I need to run composer.phar update from a web controller.

I can run all kinds of regular commands in this way (ls, cp, etc) but when I invoke the phar file I get empty output.

The code I have looks like this:

class Maintenance_Controller
{
    public function do_maintenance()
    {
        echo exec("/usr/bin/env php composer.phar", $out, $ret);
        var_dump($out); // outputs -> array()
        var_dump($ret); // outputs -> int(127)
    }
}

127 indicates a bad path, but I am sure I'm in the right directory.

Also, this works when using a php_cli wrapper, so maybe it has to do with the www-data user? chmod 777 does not help, and I hate to do that anyway.

I have also used passthru(), system() and the backtic syntax. I am unable to get to the reason this doesn't work. I can't seem to interrogate the stderr or stdout from the exec() call beyond the 127 code.

Obvious Question:

What am I doing wrong?

Better Question:

Is there a better way to interrogate and execute .phar files from within a script?

willoller
  • 7,106
  • 1
  • 35
  • 63

1 Answers1

3

UPDATE:

From this question:

Value 127 is returned by /bin/sh when the given command is not found within your PATH system variable and it is not a built-in shell command.

Try using exec('php composer.phar', $out, $ret); and see if that works. You might also need to use the full path to php if its in a non-standard location which you can probably get from which php.


Im not sure why you are using passthru here. I would use exec for better handling. Id use exec here instead of passthru

class Maintenance_Controller
{

    public function do_maintenance()
    {
        exec("composer.phar update", $out, $ret);
        if(!$ret) {
            // handle success
        } else {
           // handle error
        }

    }
}

This way you have all the output by line in $out as well as the shell return val (0 if ok, > 0 if not). If you wanna get really fancy you can loop over $out and scan for the errors and then build and exception to throw.

Community
  • 1
  • 1
prodigitalson
  • 60,050
  • 10
  • 100
  • 114