3

my idea is to start a minecraft server with the Symfony2 Process Class and want to give feedback to me in real time. So, like described in the Process cookbook part, I try the following code:

$process = new Process('sudo java -jar -Xms512M -Xmx1G ../server/minecraft_server.jar');
    $process->setTimeout(null);
    $process->run(function ($type, $buffer) {
        if ('err' === $type) {
            echo 'ERR > '.$buffer;
        } else {
            echo 'OUT > '.$buffer;
        }
    });

Because of some permission issues with the apache2 user i modified the sudoers file with this: www-data ALL = (myspecialUser) NOPASSWD: /usr/bin/java
so the www-data user can run the java command.

The server is starting in the background, but my problem is now that I'm not getting any real-time output. Only if I shutdown (or kill) the minecraft server process I get the output.

Any suggestions how to get a real-time output?

luchaninov
  • 6,792
  • 6
  • 60
  • 75
F481
  • 990
  • 8
  • 22
  • Where is the output going to? If it's a file, then you should read that file content from an action and return it, then call it periodically from ajax to have it on your page. – jaudette Dec 09 '12 at 11:50
  • Working solution will be redirect output to standard error. For example this will work for you: fprintf(STDERR, "ERR > %s", $buffer); But i'm not sure if it's the best solution – Cyprian Dec 09 '12 at 12:47
  • no, that doesn't work for me. I think I'll write a symfony task that starts the minecraft server (output on console is working) – F481 Dec 12 '12 at 11:02
  • I have the same problem local on OSX the command run for the idle timeout... and on ubuntu the process starts and don't close after the execution. Any solutions? – René Höhle Jul 28 '15 at 20:47

1 Answers1

0

Instead of calling the run() method, you should try with the start() one:

$process = new Process('sudo java -jar -Xms512M -Xmx1G ../server/minecraft_server.jar');
$process->setTimeout(null);
$process->start(function ($type, $buffer));

echo 'OUT >' . $process->getOutput();

http://api.symfony.com/master/Symfony/Component/Process/Process.html#method_start

http://api.symfony.com/master/Symfony/Component/Process/Process.html#method_getOutput

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • 2
    Neither run() nor start() works for me. In both cases, the callbacks are executed not until the process finishes. – Davincho Jan 02 '14 at 00:01
  • 2
    EDIT: Okay it seems like a Windows bug where fread blocks further execution. "Process::start is not working correctly because any call to a method that read pipes is blocking", for more details have a look at https://github.com/symfony/symfony/issues/9755 – Davincho Jan 02 '14 at 00:25