简体   繁体   English

使用php执行shell脚本

[英]Executing shell script using php

I have a shell script deploy.sh that has the following content:- 我有一个shell脚本deploy.sh ,它包含以下内容: -

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. 但是,我编写了一个PHP脚本,我可以通过http来调用deploy.sh脚本。 Content of this php page is as follows:- 这个php页面的内容如下: -

$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:- 当我通过浏览器调用这个php文件时,shell脚本实际上被调用了,我得到以下输出: -

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. 问题是eval "git pull -u origin master"命令没有执行,它的输出也没有显示。 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. 在此之前,请确保该文件具有chmod 777权限。

You should try to avoid running shell commands in php. 你应该尽量避免在php中运行shell命令。

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 根据: 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. 使用exec()函数可以做的一件事是传递两个可选值以获得更多洞察力。

Here's some code I use to test shell scripts from a web interface. 这是我用来从Web界面测试shell脚本的一些代码。

<?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. File正在使用我在其中使用的项目中的可渲染类 ,但您可以将字符串输出放在您使用它的任何位置或者echo/print_r() Also make sure you're not in safe mode by running phpinfo(); 还要确保运行phpinfo()时不处于安全模式; lots of folks having that issue. 很多人都有这个问题。

Additionally, there's no reason you should avoid using shell scripts in PHP. 此外,没有理由避免在PHP中使用shell脚本。 PHP being a scripting language, it is quite thrifty at aggregating many shell scripts to allow higher-level administration. PHP是一种脚本语言,在聚合许多shell脚本以允许更高级别的管理方面非常节俭。

PHP isn't only for 'web sites'. PHP不仅适用于'网站'。 Even then, exposing administrative scripts to web interfaces is quite useful in and of itself; 即便如此,将管理脚本暴露给Web界面本身也是非常有用的; occasionally this is even a project requirement. 偶尔这甚至是项目要求。

This is the correct code 这是正确的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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