1

I am using the Symfony 4 Process component to run various ssh commands. This all worked last fall and fails now.

The examples here are all real code (except for the remote host and username). This is the baseline:

<?php
namespace App\Command;

use ...

class TestCommand extends Command
{
  public static $defaultName = 'app:test';

  public function configure()
  {
    $this->setDescription( 'Test command for Process problem' );
  }

  protected function execute( InputInterface $input, OutputInterface $output )
  {
    $host = 'dpatterson@webhost01.dplhenterprises.com';
    $command =
    [
      // Command, arguments, and options.
    ];

    $process = new Process( $command );
    $process->run();
    $output->writeln( 'Output: ' . $process->getOutput());
    $output->writeln( 'Errors: ' . $process->getErrorOutput());
  }
}

For the remainder of this post I'll present the value of $command and the output.

$command as command and argument together:

$command = ['/usr/bin/ssh ' . $host . ' "if [ -d /admin ]; then echo 1; else echo 0; fi"'];

Output:
Errors: sh: /usr/bin/ssh user@example.com "if [ -d /admin ]; then echo 1; else echo 0; fi": No such file or directory
sh: line 0: exec: /usr/bin/ssh user@example,com "if [ -d /admin ]; then echo 1; else echo 0; fi": cannot execute: No such file or directory

$command is separate command and argument:

$command =
[ 
  '/usr/bin/ssh',
  $host . ' "if [ -d /admin ]; then echo 1; else echo 0; fi"',
];

The process "'/usr/bin/ssh' 'user@example.com "if [ -d /admin ]; then echo 1; else echo 0; fi"'" exceeded the timeout of 60 seconds.

$command as three separate items:

$command =
[
  'ssh',
  $host,
  '"if [ -d /admin ]; then echo 1; else echo 0; fi"',
];

Output:
Errors: bash: if [ -d /admin ]; then echo 1; else echo 0; fi: No such file or directory

Again, this worked with the first configuration in the fall of 2018. I honestly don't know what version of Symfony I was using at the time, but I'm pretty sure it was at least 4.1.

Environment:
- macOS High Sierra 10.13.6
- PHP: 7.2.14
- Symfony 4.2.2

TIA

David Patterson
  • 1,780
  • 3
  • 17
  • 40

1 Answers1

0

Your second try seems to work fine, just set set_time_limit(0); and also $process->setTimeout(null); before calling $process->run().

To debug your process command, dump $process->getCommandLine() to see how it's generated.

Bloops
  • 744
  • 1
  • 11
  • 15
  • 1
    Curious. It's not working for me. I think I may be getting an ssh timeout. This is the error I receive: [ssh: connect to host example.com "if [ -d /admin ]; then echo 1; else echo 0; fi" port 22: Operation timed out]. Oddly enough, If I copy the command from the error and issue it directly, it works fine and fast. – David Patterson Mar 08 '19 at 22:35