Jump to content

[SOLVED] CLI: using proc open to run an interactive command...


Recommended Posts

Hi, I'm trying to have a php cli script execute and wait for the termination of vi using proc_open however vi never opens.  Here is the code...

 

$arr_pipes = array( );

$str_command = 'vi /etc/passwd';

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

$process = proc_open($str_command, $descriptorspec, $pipes);

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

  fclose($pipes[0]);
  fclose($pipes[1]);

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

 

Anyone have any suggestions?  I'm using ssh (Putty) and my shell is csh.

 

Stumbled upon the answer... file and "php: ... " must be used for both descriptor 0 and 1.

 

$arr_pipes = array( );

$str_command = 'vi /etc/passwd';

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

$process = @proc_open( $str_command, $descriptorspec, $arr_pipes );

if ( is_resource( $process ) ) {
  $return_value = proc_close( $process );
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.