Jump to content

proc_open help needed...


Caliber Mengsk

Recommended Posts

Ok, so this may seem a little odd, but I'm trying to make a web based console for minecraft using php and ajax. I make it sound so simple. I've gotten proc_open to open the server, and I can read (somewhat) what comes out, but, it just locks up while reading out, until the process timeout hits.

 

I know I'm getting some of the data (first two lines at least), then the server goes into it's waiting line (where it times out). When getting the pipe information, it reads a whole bunch of > symbols. This is because normally the server would be printing a > that blinks to say waiting for input.

 

Also, I can't send commands to the program after it's open this way.

 

Pretty much, I am needing the process to stay open as a session, or I need some way to access an already open process. Any ideas?

 

Here's the code that I've got right now:

<?php
session_start();
$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("pipe", "w")//, "a") // stderr is a file to write to
);

$cwd = '';

$_SESSION['process'] = proc_open('java -Xincgc -Xmx1G -jar "C:\Users\Rob\Dropbox\Other Stuff\minecraft\craftbukkit-1.8.1.jar"', $descriptorspec, $pipes);

if (is_resource($_SESSION['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], '');
    fclose($pipes[0]);
$last_returned = "";
stream_set_read_buffer($pipes[1], 1);
while($returned = stream_get_contents($pipes[1]))
{
	echo $returned;
}


    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($_SESSION['process']);

    echo "Waiting for new commands...";
}
?>[code]

which outputs:
[code]
161 recipes 17 achievements > > > > > > > > > > > >Waiting for new commands...

 

So, again, any ideas?

Link to comment
Share on other sites

Pretty much, I am needing the process to stay open as a session

Not possible: PHP does not let you keep resources open after the script has terminated.

 

or I need some way to access an already open process.

As far as I know the only way you can really do this (without using some external program) would require the pcntl extension, which isn't available when you're running from Apache - not to mention it isn't available for Windows at all.

 

You could make a PHP script run as a "server" on a specific port that acts as a bridge between Bukkit/Minecraft and your other scripts. It opens a port and listens for commands, and your normal scripts connect to that port and send commands. The server script would keep running so it can keep the one process running and communicating.

Or find a way to keep the Bukkit thing running in the background and communicate with that in a similar way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.