I have a shell script deploy.sh that has the following content:-
echo "0 Importing the code"
eval "git pull -u origin master"
echo "1 Backing up existing data in database.."
// -- other code follows here
When I execute the script directly using the terminal, I get the following output:-
0 Importing the code
remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From bitbucket.org:user/repo * branch master -> FETCH_HEAD Updating db13xxx..6705xxx
1 Backing up existing data in database..
This is correct. However, I wrote a PHP script with which I can invole the deploy.sh script over http. Content of this php page is as follows:-
$output = `./deploy.sh`;
echo '<pre>', $output, '</pre>';
When I invoke this php file through the browser, the shell script is in fact getting invoked and I'm getting the following output:-
0 Importing the code 1 Backing up existing data in database..
The problem is that the eval "git pull -u origin master" command didnt get executed and its output is not shown. Any idea what the problem is?
4 Answers 4
This works
<?php
$output = shell_exec('sh deploy.sh');
echo "$output";
?>
Before that make sure that file has chmod 777 permission.
Comments
One thing you can do with the exec() function is to pass two optional values for more insight.
Here's some code I use to test shell scripts from a web interface.
<?php
require_once(__DIR__.'/../libs/Render.php');
error_reporting(E_ALL);
//Initialize and Run Command, with a little trick to avoid certain issues
$target='cd ../../your/relative/path && ./CustomScript.sh';
$outbuf=exec($target,$stdoutbuf, $returnbuf);
//Structure
$htm= new renderable('html');
$html->children[]= $head= new renderable('head');
$html->children[]= $body= new renderable('body');
$body->children[]= $out= new renderable('div');
$body->children[]= $stdout= new renderable('div');
$body->children[]= $returnout= new renderable('div');
//Value
$out->content= 'OUTPUT: '.$outbuf;
$stdout->content= 'STDOUT: '.var_export($stdoutbuf,true);
$returnout->content= 'RETURN: '.$returnbuf; //127 == Pathing problem
//Output
print_r($html->render());
?>
File is using the renderable class from the project I use this in, but you can put the string output wherever you are using it or echo/print_r() just as well. Also make sure you're not in safe mode by running phpinfo(); lots of folks having that issue.
Additionally, there's no reason you should avoid using shell scripts in PHP. PHP being a scripting language, it is quite thrifty at aggregating many shell scripts to allow higher-level administration.
PHP isn't only for 'web sites'. Even then, exposing administrative scripts to web interfaces is quite useful in and of itself; occasionally this is even a project requirement.
Comments
You should try to avoid running shell commands in php.
Having said that, try this:
$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";
As per: http://www.php.net/manual/en/function.shell-exec.php
2 Comments
This is the correct code
<?php
$cmd = 'ifconfig'; // pass command here
echo "<pre>".shell_exec($cmd)."</pre>";
?>
1 Comment
ipconfig.
evalnecessary? Couldn't you just runecho '0'; git pull -u origin master; echo '1'` etc.#!/bin/bashto the top of the script?