Jim_CS
Jim_CS

Reputation: 4172

Multiple writes to a process opened with proc_open

So I open a process with $process = proc_open("my_process", $descriptors, $pipes);

Then I write to the stdin of the process using fwrite($pipes[0], "some_command");

Then I have to close the pipe using fclose($pipes[0]); before i can read from the pipes stdout using $output = stream_get_contents($pipes[1]);. If I don't close the pipe my php script hangs on this call.

But once I have received the output from stdout what if I want to send another command to the process...the stdin pipe is closed so I have no way to send it. So is it possible to somehow send another command to the process?

Upvotes: 4

Views: 2423

Answers (1)

DaveRandom
DaveRandom

Reputation: 88707

It sounds like the other process is blocking waiting for EOL or EOF on STDIN. What are you trying to execute?

Regardless, there's a pretty good chance this will sort it out: Just append \n to the command you are sending to the other process.

E.g.

$process = proc_open("my_process", $descriptors, $pipes);

$command = "some_command";
fwrite($pipes[0], $command."\n");

// Fetch the contents of STDOUT

Now, one issue that you may also be running into is to do with the fact that you are using stream_get_get_contents() - which will wait for EOF before it returns. You may have to be a bit more intelligent about how your retrieve the data from $pipes[1], using fgets() and looking for a specific number of lines or a string to indicate the end of the output.

If you tell us what you are executing, I may be able to give you a more specific answer.

Upvotes: 2

Related Questions