3

I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:

<html>
<body>
<?php
    $res = shell_exec("/var/www/html/run.sh");
    echo $res . " is the result";
?>
</body>
</html>

If that html file is called on firefox, the output is

 is the result

If I directly run php with that file (php index.php), the output is

<html>
<body>
13.00
 is the result</body>
</html>

So, where does the result get lost?


edit: The webpage source code from firefox is

<html>
<body>

 is the result</body>
</html>

edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.

user236012
  • 265
  • 1
  • 2
  • 9
  • Try to run with `$res = shell_exec("sh /var/www/html/run.sh");` – Hardik Solanki Dec 24 '15 at 11:26
  • Take a look here: http://stackoverflow.com/a/20109859/1973205 – Theraloss Dec 24 '15 at 11:28
  • 1
    The exec functions "only" return the contents of stdout. Do you get an error message when you redirect stderr to stdout? `$res = shell_exec("/var/www/html/run.sh 2>&1");` – VolkerK Dec 24 '15 at 11:28
  • @VolkerK - that's it, thanks. the shell script uses '~', which is expanded to correct directory when running the php directly in bash, but directs to the wrong directory when the webserver runs the script. stupid me ;) – user236012 Dec 24 '15 at 11:33
  • SImpler than expected ;-) I'll make it an answer then. – VolkerK Dec 24 '15 at 11:37

2 Answers2

1

The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via

$res = shell_exec("/var/www/html/run.sh 2>&1");
Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

shell_exec does not run if you're in safe mode, that could be the issue

jean
  • 4,159
  • 4
  • 31
  • 52
user5697101
  • 571
  • 4
  • 12