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?

3
  • Is the eval necessary? Couldn't you just run echo '0'; git pull -u origin master; echo '1'` etc. Commented Feb 25, 2013 at 5:38
  • @Alison, after u said, I tried removing the eval. It still runs when directly invoked, but same result when run through php page. Commented Feb 25, 2013 at 5:44
  • 1
    Have you tried adding #!/bin/bash to the top of the script? Commented Feb 25, 2013 at 7:57

4 Answers 4

4

This works

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

Before that make sure that file has chmod 777 permission.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

2 Comments

Updated the code, is there an error message coming up when you try to run the php script at the moment?
@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)
-2

This is the correct code

<?php 
  $cmd = 'ifconfig'; // pass command here
  echo "<pre>".shell_exec($cmd)."</pre>";
?>

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.