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?

asked Feb 25, 2013 at 5:29
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.

answered Aug 19, 2014 at 9:23
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.

answered Nov 16, 2013 at 13:22

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

d-_-b
23.2k43 gold badges174 silver badges291 bronze badges
answered Feb 25, 2013 at 5:32

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>";
?>
answered Mar 18, 2016 at 12:12

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.