RockOnGom
RockOnGom

Reputation: 3961

Can't run bash script in PHP

I'm trying to run bash script in PHP but can't run it. php -v

PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:19:58)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Ubuntu 12.04 LTS 64 bit.

My php code:

    $cmd='/www/var/pl/bash.sh';
    $retval =-1;
    exec( $cmd, $output ); //executing the exec function.
    foreach( $output as $tmp ) 
    { 
        echo "$tmp <br>"; 
    };

bash.sh:

 #!/bin/bash
swipl --quiet -s /var/www/pl/ples.pl -g "f(R, gel), writeln(R),open('/var/www/pl/in.txt',write, Stream),
write(Stream, (R)),
nl(Stream),
close(Stream)" -t halt.

What am I doing wrong?

And I can run bash.sh in the Linux terminal.

Upvotes: 5

Views: 3511

Answers (2)

JvdBerg
JvdBerg

Reputation: 21866

When you run the script in the terminal you are executing it under the account you are logged in to. You have a shell setup with a search path etc.

When php executes the script, it has not a shell setup, and runs under the webserver user account. When executing:

  • make sure you have complete paths to your file, swipl is not enough, it should be /path/to/swipl
  • make sure the webserver process has enough access rights to get everything it needs.

Upvotes: 5

LSerni
LSerni

Reputation: 57428

Most likely it is either a path or permission problem; for example the user the web application runs as, has no idea where the swipl program is.

Add 2>&1 to the command line before exec'ing it, so that it tells you what the problem is. Or you can find the stderr output into the web server error log (or PHP error log; check its path and settings in php.ini).

Upvotes: 0

Related Questions