CMD from PHP script - get feedback

156 views Asked by At

I have a LOT ( almost 300 ) old SVN repositories to migrate to git using git2svn.

After considering GOLANG and PYTHON, I finally decided that the easiest way is to use PHP . Might be a bad questionable decision, but it's seemed easy.

So, after 15 minutes , I did have a script that is more or less running ok in tests . Ugly script , but it is a one-timer.

The problem is that the process takes a lot of time , even for simple almost empty repos is can take 30sec. and even a minute. On big ones - even 10min - so before taking it into production, I would like to have some feedback mechanism - so I can actually see what is going on .

..as of now ,the script does output the command feedback like so :

$cmd = "cd ".$GITrepoPath." && svn2git svn://127.0.0.1/". $repoName . " --username " .$SVNusername ." --authors authors.txt --notags --nobranches --notrunk";
            $output = shell_exec($cmd);
            echo "<pre>$output</pre>";

..but this is only after each repo was finished processing .. not like the real cmd execution where I can see the steps .

The only question I found that might be close to what I need was here - but honestly - I did not understood much from the answer ...

I know it is just a one-timer script - but the use case had me interested in how to actually achieve that ( and if it is possible ).

I am on a win7 local machine , but would like to know also for *nix if possible .

2

There are 2 answers

0
Maxie On BEST ANSWER

shell_exec waits until the process closes. You have to create the process and listen to it, the same as CMD. Use exec function in this way:

$cmd = ''; // your command here
$output_storage = [];
$output_showed = [];
$result = null;
exec($cmd, $output_storage, $result);
while( $result === null ){
    $diff = array_diff($output_storage, $output_showed);
    if( $diff ){
        // all new outputs here as $diff
        $output_showed = $diff;
    }
}
0
NibbleBits On

I suggest instead running a script or program in the background that runs the command and then updates a record in a database, you could then use AJAX or whatever to poll the server for record changes. This allows a nice environment for the user.

The column in the database table could be named something like "finished" and once that boolean is true then you know its complete and the output could be stored in the database.