Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm on CentOS 6 64bit with PHP 5.4.12.

This is very strange behavior by PHP.

See the following script:

echo 'Trying Query: whereis jbig2dec<br>';
exec('whereis jbig2dec',$output) or die('Failed');

$location=explode(' ',implode($output));

echo 'Result: '.implode($output)."<p>";

echo 'Trying Query: '.$location.' --help<br>';
exec($location.' --help',$output) or die('Failed');

echo 'Result: '.implode($output)."<p>";

echo 'Trying Query: jbig2dec --help<br>';
exec('jbig2dec --help',$output) or die('Failed');

echo 'Result: '.implode($output)."<p>";

The result is:

Trying Query: whereis jbig2dec
Result: jbig2dec: /usr/local/bin/jbig2dec
Trying Query: /usr/local/bin/jbig2dec --help

Basically you can see above that PHP exec function works for whereis jbig2dec but fails for /usr/local/bin/jbig2dec --help. BUT if I type /usr/local/bin/jbig2dec --help or jbig2dec --help into SSH I get the correct response from jbig2dec.

How could this be?

share|improve this question
up vote 4 down vote accepted

Check the return value of /usr/local/bin/jbig2dec --help over SSH like this:

$ /usr/local/bin/jbig2dec --help
[... generates some output ...]
$ echo $?

The last command prints the "return value" of the previous call. A return value of 0 is "the shell way" of saying "the command executed successfully". A return value of something else indicates an error.

If exec() encounters a return value that is not 0 (which some programs give on help messages, since the program did not technically run), it will return "false" in the PHP context and thus trigger the or die() part and terminate the PHP script with the given error message.

In shorter words: your command probably does not return a successful status, and you have told PHP to die() if it doesn't.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .