title: Controlling an interactive process with PHP using Symfony Process
body: <p>I am trying to control the systems <a href="http://linux.die.net/man/1/ssh-add" rel="nofollow">ssh-agent</a> by adding new keys to it using <a href="http://linux.die.net/man/1/ssh-add" rel="nofollow">ssh-add</a>. For this I'm using the <a href="http://symfony.com/doc/current/components/process.html" rel="nofollow">Symfony Process</a> component.</p>
  
  <p>When I run this code from a web site it works perfectly fine but when I run the same code in a shell/console the ssh-add process hangs on <code>Enter passphrase for &lt;path to key&gt;:</code></p>
  
  <p>A simplified version of the code looks something like this</p>
  
  <pre><code>use Symfony\Component\Process\Process;
  
  $keyPath = '&lt;path to key&gt;';
  $keyPassword = '&lt;password for unlocking the key&gt;';
  $socketPath = '&lt;path to ssh-agent socket&gt;';
  
  $sshAdd = new Process(
      "ssh-add {$keyPath}",
      null,
      [
          'SSH_AUTH_SOCK' =&gt; $socketPath
      ],
      $keyPassword
  );
  $sshAdd-&gt;run();
  </code></pre>
  
  <p>As you can see in the code above I make a call to <code>ssh-add</code>, sets the <code>SSH_AUTH_SOCK</code> in the environment so <code>ssh-add</code> can talk to the agent and then sends the password in the input. As I said previously, when I run this in a web context it works but it hangs in a shell/console context.</p>
  
  <p>I did an <code>strace</code> of when running in the console and the relevant parts looks like this</p>
  
  <pre><code>open("&lt;path to key&gt;", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
  write(4, "&lt;key password&gt;", &lt;length of password&gt;)      = 20
  close(4)                                              = 0
  wait4(9650, 0x7fff00ab3554, WNOHANG|WSTOPPED, NULL)   = 0
  select(8, [5 7], [], [], {0, 0})                      = 0 (Timeout)
  wait4(9650, 0x7fff00ab3554, WNOHANG|WSTOPPED, NULL)   = 0
  select(8, [5 7], [], [], {0, 0})                      = 0 (Timeout)
  select(8, [5 7], [], [], {0, 200000}Enter passphrase for &lt;path to key&gt;:) = 0 (Timeout)
  select(8, [5 7], [], [], {0, 200000})                 = 0 (Timeout)
  select(8, [5 7], [], [], {0, 200000})                 = 0 (Timeout)
  select(8, [5 7], [], [], {0, 200000})                 = 0 (Timeout)
  select(8, [5 7], [], [], {0, 200000})                 = 0 (Timeout)
  ...
  </code></pre>
  
  <p>As you can see the write seems to be ignored and the <code>ssh-add</code> program starts to block waiting for input.</p>