3

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?

Sparky
  • 4,769
  • 8
  • 37
  • 52
  • Is the `eval` necessary? Couldn't you just run `echo '0'; git pull -u origin master`; echo '1'` etc. – Armand Feb 25 '13 at 05:38
  • @Alison, after u said, I tried removing the eval. It still runs when directly invoked, but same result when run through php page. – Sparky Feb 25 '13 at 05:44
  • 1
    Have you tried adding `#!/bin/bash` to the top of the script? – Armand Feb 25 '13 at 07:57

4 Answers4

4

This works

<?php
$output = shell_exec('sh deploy.sh');
echo "$output";
?>

Before that make sure that file has chmod 777 permission.

Swetha
  • 746
  • 10
  • 19
3

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

d-_-b
  • 21,536
  • 40
  • 150
  • 256
ChrisK
  • 1,392
  • 10
  • 12
  • Updated the code, is there an error message coming up when you try to run the php script at the moment? – ChrisK Feb 25 '13 at 05:40
  • @Sparky Apologies, try with the updated function - This one should give some sort of feedback. Probably may not run still but may get you closer. (the issue I suspect is a pathing error) – ChrisK Feb 25 '13 at 11:41
3

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.

-2

This is the correct code

<?php 
  $cmd = 'ifconfig'; // pass command here
  echo "<pre>".shell_exec($cmd)."</pre>";
?>
Vikash Jha
  • 67
  • 1
  • 7
  • This does not answer the question. The (3-year old) question concerned a specific batch job, that ran, but only partially. It's not about `ipconfig`. – trincot Mar 18 '16 at 12:48