0

I am currently running a Minecraft server in a screen session with this command:

(tail -f /path/to/fifo & cat) | java -Xmx2048M -jar minecraft_server.jar nogui

You can shutdown a minecraft server by sending 'stop' in the server console. I am using the fifo to send commands from other bash scripts, and cat to allow input from the actual Minecraft server console in the screen session.

What happens though, is that if you put the command 'stop' in the actual minecraft console, the server ends up hanging right before it should exit because of the 'cat' command. The only way to get past this, is to press enter again after sending the stop command.

How can I get 'cat' to not cause this to hang?

Edit: The full script.

#!/bin/bash

serverDirectory=/opt/games/minecraft
pidFile=$serverDirectory/server.pid
fifoFile=$serverDirectory/server.fifo

cleanup() {
    rm -f $pidFile
    rm -f $fifoFile
}

if [ ! -p $fifoFile ]; then
    mkfifo $fifoFile && chmod 0777 $fifoFile
fi

echo $$ > $pidFile

# restart server if it stops
while true
do
    # how minecraft server should handle an interruption
    trap "{ echo 'stop' > $fifoFile ; }" SIGINT
    (tail -f $fifoFile & cat) | java -Xmx2048M -jar minecraft_server.jar nogui
    echo "Restarting server...."
    # if interruption occurs before we restart, stop trying to restart and clean up
    trap "{ cleanup ; exit 0 ; }" SIGINT SIGTERM
    sleep 5
done
Eddy
  • 1
  • 2

1 Answers1

1

I haven't used a minecraft server, so I don't know if I'm on the right track here, but would this work?

#!/bin/sh

fifo="/path/to/fifo"
mkfifo $fifo
trap "rm -f $fifo" 0 1 2 3 6 15

/path/to/java -Xmx2048M -jar minecraft_server.jar nogui < $fifo &
echo $? > /path/to/minecraft.pid
cat > $fifo

This still doesn't kill off the cat once the server quits, but at least it doesn't block the server. You might want to launch the minecraft server in a function that kills the cat when it exits. I suggest keeping the .pid file for possible future use. :-)

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • Thanks. I might switch the script to putting the java process in the background. My problem is that the Minecraft server never actually exits because of the cat. Without cat, it will exit just fine. I can tell this because there should be some output like "Restarting" whenever the server exits, that doesn't happen with cat. I've edited original post w/ the full actual script I am using. When I switched it to use your way, the same thing happened. – Eddy May 28 '12 at 03:17
  • I figured that the goal of your script was to maintain *two* ways of getting commands into minecraft-server's standard input, so I just tried to achieve that using a different method. I suspect that if the "same thing happened", then actually something else happened that just looked the same. :) Did minecraft-server.jar fail to exit, or was it the wrapper script that didn't exit because nothing was around to kill the `cat`? – ghoti May 28 '12 at 11:04
  • Oh wow. I see exactly what you are asking now (not sure if because of your wording or because I slept since). Apologies for not seeing this sooner. It is infact because nothing is around to kill the cat. If I am running the java process in the background, what would be the best way to check if the server is still running? Put the "cat > $fifo" part in a loop while the server pid exists? – Eddy May 28 '12 at 20:28