简体   繁体   中英

Executing shell script using php

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?

This works

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

Before that make sure that file has chmod 777 permission.

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

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.

This is the correct code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM