exec() not working as expected in PHP
Question
I have a PHP script in which I need to execute a shell script 'add_article.sh' which reads a parameter file and adds the news article contents to a search index (Lemur/Indri).
Executing the 'add_article.sh' script on its own (from the shell) works perfectly, but running it from within the php script I get this:
$blah = exec("./add_article.sh", $out, $ret_val);
echo $out . "<BR />";
echo $ret_val . "<BR />";
This produces
Array
255
It is in the correct directory and fails even with an absolute path. I use exactly the same format in another function in the same file that executes another script in the same directory and all is fine.
Any ideas?
Solution
Have you checked the execute permissions of your shell script? Apache runs as a user with very few permissions in most operating systems, so that may be the cause.
OTHER TIPS
$out
is supposed to be an array. You should probably print_r()
or var_dump()
it to see what's coming back from the script; it may be telling you what's going wrong.
In general, there's probably some environmental dependency that isn't being satisfied when PHP is running the script. This is especially common if it's being run from inside Apache.
Try:
$blah = exec("./add_article.sh", $out, $ret_val);
print_r($out);
echo '<br />';
echo $ret_val . "<br />";