Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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));
$location=$location[1];

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
Failed

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.