69

I have a bash script, that I run like this via the command line:

./script.sh var1 var2

I am trying to execute the above command, after I call a certain php file.

What I have right now is:

$output = shell_exec("./script.sh var1 var2");
echo "<pre>$output</pre>";

But it doesn´t work. I tried it using exec and system too, but the script never got executed.

However when I try to run shell_exec("ls"); it does work and $output is a list of all files.

I am not sure whether this is because of a limitation of the VPS I am using or if the problem is somewhere else?

r0skar
  • 8,338
  • 14
  • 50
  • 69
  • 1
    What path are you running it in? What does `pwd` return? – alex Jun 15 '12 at 14:05
  • 1
    Is your script executable by `apache` or `www-data` user? – core1024 Jun 15 '12 at 14:06
  • 2
    Is that bash script in the same directory as your PHP script? Is the php script's working directory that same directory as well? – Marc B Jun 15 '12 at 14:07
  • Does your script have an appropriate interpreter header, and can you run it manually from your terminal? Like: `#!/bin/bash` – Robert K Jun 15 '12 at 14:09
  • 1
    Does it work with `shell_exec('sh script.sh')`? – dan-lee Jun 15 '12 at 14:09
  • Thanks for your answers. 1)I can run it from terminal. 2) @DanLee Doesnt work. 3)It seems that the php file I am modifying is a shortcut, because allthough I "see" them in the same dir, `pwd` did return another dir. Is it possible to include the path in the command? – r0skar Jun 15 '12 at 14:11
  • Instead of `./script.sh` use `/path/to/script.sh`. – Dennis Williamson Jun 15 '12 at 15:03
  • I'm having this same problem. I can execute the bash script from both my home dir, and the /var/www/html/ dir. When I run the page, with 'LS' in the the exec('ls') it lists the files in the directory ... but when i put the bash file and path it doesn't work. – kloodge Nov 20 '20 at 16:05

3 Answers3

96

You probably need to chdir to the correct directory before calling the script. This way you can ensure what directory your script is "in" before calling the shell command.

$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh var1 var2');
chdir($old_path);
Robert K
  • 30,064
  • 12
  • 61
  • 79
  • I think this did the trick! At least I got it working with a simple test script. To finally verify it, I have to wait for the server hoster to let me know the exact path to the scripts. I am gonna mark the answer now nevertheless. Thanks! – r0skar Jun 15 '12 at 14:24
  • 2
    @Andrej: Or use the absolute path to your script instead of a relative one. – Dennis Williamson Jun 15 '12 at 15:01
  • @Andrej If you're running PHP 5.3, you can use `chdir(__DIR__)` to change the directory to the directory containing the script. Or for PHP 5.2 or less, `dirname(__FILE__)` will do the trick. – Robert K Jun 15 '12 at 15:07
  • Thanks guys! It is working now. There has appeared a [new problem](http://stackoverflow.com/questions/11053452/php-run-bash-script-to-create-file), but this one is solved ;) – r0skar Jun 15 '12 at 15:16
  • If you want to be pithy swap `shell_exec` out for the [backtick operator](http://devdocs.io/php/language.operators.execution). – vhs Jun 06 '17 at 02:10
5

Your shell_exec is executed by www-data user, from its directory. You can try

putenv("PATH=/home/user/bin/:" .$_ENV["PATH"]."");

Where your script is located in /home/user/bin Later on you can

$output = "<pre>".shell_exec("scriptname v1 v2")."</pre>";
echo $output;

To display the output of command. (Alternatively, without exporting path, try giving entire path of your script instead of just ./script.sh

Hrishikesh
  • 1,076
  • 1
  • 8
  • 22
  • It's a unnecessary to alter the `PATH` environment variable. The current working directory should be changed instead (see [my answer](http://stackoverflow.com/a/11052453/24950)). – Robert K Jun 15 '12 at 14:19
  • 1
    True. Thanks. I was using the path settings in my code because I have various custom executables in more than one locations, and I wanted the input box to work as a console, so I can invoke any command from those. If it is to be used for just single file execution, we can anyway just use shell_exec('/entire/path/to/file/'); – Hrishikesh Jun 15 '12 at 14:22
0

Check if have not set a open_basedir in php.ini or .htaccess of domain what you use. That will jail you in directory of your domain and php will get only access to execute inside this directory.