1

I want to execute git clone in my php. As the code below:

<?php
    exec('git clone git@github.com:xxx/xxx.git /home/xxx',
    $out);
    foreach($out as $info){
       echo $info."</br>";
    }
?>

After execute it, the target project is not clone into my local directory and the $info has no data and there are no error information.

I try git clone in my bash script, and it can achieve clone. The code as below:

#!/bin/sh
git clone git@github.com:xxx/xxx.git /home/xxx

And it seemed I can use git add in php, So why git clone is so particular that I cant execute it in my php file.

edit: I think it is not same as How can I debug exec() problems? . I find that I can execute this php with cli, in this way git clone will achieve its task. But git clone will be lazy with fpm.

nail fei
  • 2,179
  • 3
  • 16
  • 36
  • 4
    Possible duplicate of [How can I debug exec() problems?](https://stackoverflow.com/questions/12199353/how-can-i-debug-exec-problems) – user3942918 Jul 28 '17 at 07:16
  • 1
    You are not reading STDERR, so there's likely a problem and you're not getting the error message from git. Try adding `2>&1` to the end of your exec command to redirect STDERR to STDOUT. A likely culprit is that the web server user doesn't have permissions to write to `/home/xxx`. – cmbuckley Jul 28 '17 at 08:39
  • @cmbuckley. it worked, I get error message with `2>&1` **error: cannot run ssh: No such file or directory**. I am curious about that without this where the error log will be output? – nail fei Jul 28 '17 at 09:11
  • Shell commands have 3 standard streams: IN (reading content), OUT (the part captured by exec), and ERR. `2>&1` redirects ERR to OUT so that exec can capture everything. Read more in [SO Documentation](https://stackoverflow.com/documentation/bash/399/redirection/7602/stdin-stdout-and-stderr-explained#t=201707281147557457059) or [Wikipedia](https://en.wikipedia.org/wiki/Standard_streams). – cmbuckley Jul 28 '17 at 11:50

0 Answers0