Search code examples
phpshellsweave

PHP shell_exec not working


I'm trying to run a shell script on my MacOSX, but it's not working. My report.sh file calls another .sh file (Sweave.sh) that is stored in /usr/local/bin.

<?php

shell_exec("./report.sh");
echo "Hello 123";

?>

It print's 'Hello 123' on my browser, but report.sh is not doing it's job. Why that happens? Here's report.sh:

# !/bin/bash
Sweave.sh -ld test_sweave.Rnw

If if call ./report.sh from the terminal, it works ok...thanks for any help!

EDIT: If i call ./report.php from the terminal, it says:

./report.php: line 2: ?php: No such file or directory
./report.php: line 4: syntax error near unexpected token `"./report.sh"'
./report.php: line 4: `shell_exec("./report.sh");'

Solution

  • If you want to run your script via command line you have to either:

    a) start the file with this line

    #!/path/to/php
    

    b) run as:

    php report.php
    

    If that works, it could be due to permissions, since your web server runs under a different user than you.