1

I'm using passthru to run scp. Normally scp outputs a progress bar, but it's not being drawn when I use passthru. I want some estimation of how long the transfer will take. Is there a way to force it to show?

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    I'm sure `scp` detects its output is not PTY. You should have better luck with expect: http://php.net/manual/en/book.expect.php – Marek May 22 '15 at 16:59

1 Answers1

2

Most programs which are linked against libc use the function isatty to check whether stdout is a terminal before they decide to colorize their output. Thus to make sure that the ANSI terminal escape sequences will not screw up a pipe or get redirected into a file. passthru() will not run the command in a terminal.

In PHP you can use proc_open() to open a process and present it a terminal for stdout. Take this example from the manual, which I have modified to use a pty instead of a pipe for stdout and stderr:

$descriptorspec = array(
   0 => array("pipe", "r"), // stdin is a pipe that the child will read from
   1 => array("pty", "w"),  // stdout is a pty that the child will write to
   2 => array("pty", "w")   // stderr is a pty that the child will write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('command', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    echo stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}

However you can also use LD_PRELOAD when starting a process and trick the program in a way that it thinks stdout is a terminal. (hackish but sometimes the last resort). I've described that here: Bash: trick program into thinking stdout is an interactive terminal

Community
  • 1
  • 1
hek2mgl
  • 152,036
  • 28
  • 249
  • 266