Run Bash Command from PHP Run Bash Command from PHP bash bash

Run Bash Command from PHP


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);


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/binLater 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


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.