Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 914
RockOnGom Avatar asked Sep 22 '12 10:09

RockOnGom


People also ask

How to run a bash script using php?

I have below php file to run a bash script... <? php $old_path = getcwd(); chdir('/home/scripts/'); $var = escapeshellarg("PC7"); $output = shell_exec("./Script2.sh $var"); chdir($old_path); echo $output; ?>

Can PHP run a shell script?

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string.

What is $_ bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

How do I open a link in bash?

xdg-open - opens a file or URL in the user's preferred application xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. If a file is provided the file will be opened in the preferred application for files of that type.


1 Answers

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.
like image 131
JvdBerg Avatar answered Sep 22 '22 14:09

JvdBerg