Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start and stop minecraft (java process) via php [closed]

OK, let me try again. :-)

If I try:

<?php
exec("java -Xms1024M -Xmx1024M -jar craftbukkit-1.4.2-R0.2.jar -o true");
?>

The web page never returns. I believe it is because the CGI is waiting for the process to complete, which of course it never does because it is a server.

I try this locally and if I put the same command in the terminal it runs as expected so I know there is no problem with the command line.

So...the question is:

1) does exec block until the process completes? 2) if no, what is happening here, if yes, how can I change that behavior?

=============================================================

So this is really a generic question, but I have included to the specific environment which might help to explain why I am trying to do this. This is all on LINUX.

I want to launch a Java process remotely from a PHP page. The trick of course is that the process will keep running and I want to the PHP page complete once the process has successfully launched.

So, first how do I do that? I've tried a number of things I've read and can't get them to work.

The second part is a little more tricky. The way you stop the minecraft server, is in the terminal you type "Stop". And of course you can enter other commands there to do other things...its just reading from stdin.

If I want to stop it, or send it other commands, I need to connect to stdin from a PHP script and send it text or something.

When I launch the java process, do I connect stdin to a file and then from future PHP scripts write to the file? I don't know.

Just give me a few pointers on the right approach and will try them. I'll let you know if they work or if I have more detailed questions.

Thanks!

like image 973
DungeonTiger Avatar asked Aug 24 '26 06:08

DungeonTiger


1 Answers

In php you can use the function exec(); i.e.:

<?php
    if($method == "start"){
        exec("java -jar minecraft_server.jar");
    }else if($method == "stop"){
        exec("pkill java");
    }
?>

I am not familiar with the exact commands for Minecraft, but this is the theory behind it.

EDIT

Have you ever used the program 'screen'?

Create a new detached screen running the Minecraft Server using the following command:

screen -dmS Minecraft /home/username/minecraft/launch.sh

This will create a new screen named Minecraft, and execute launch.sh, the script that comes with the minecraft server. Thiss will all now run in the background, and not keep a shell occupied.

To send commands to this running screen, you can then use:

screen -S Minecraft -X stuff $'stop\n'

To send a stop command.

like image 84
Matt Clark Avatar answered Aug 25 '26 20:08

Matt Clark