70

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?

8
  • 1
    What path are you running it in? What does pwd return? Commented Jun 15, 2012 at 14:05
  • 1
    Is your script executable by apache or www-data user? Commented Jun 15, 2012 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? Commented Jun 15, 2012 at 14:07
  • Does your script have an appropriate interpreter header, and can you run it manually from your terminal? Like: #!/bin/bash Commented Jun 15, 2012 at 14:09
  • 1
    Does it work with shell_exec('sh script.sh')? Commented Jun 15, 2012 at 14:09

3 Answers 3

98

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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!
@Andrej: Or use the absolute path to your script instead of a relative one.
@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.
Thanks guys! It is working now. There has appeared a new problem, but this one is solved ;)
If you want to be pithy swap shell_exec out for the backtick operator.
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

2 Comments

It's a unnecessary to alter the PATH environment variable. The current working directory should be changed instead (see my answer).
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/');
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.